2015-07-03 58 views
0

我需要在Silverlight列表框中顯示WCF服務返回值(LIST)。ObservableCollection沒有綁定列表框中的數據

創建GetAllAgents類一樣,

public class GetAllAgents 
    { 
     public List<string> FirstName { get; set; } 


     public GetAllAgents(List<string> firstName) 
     { 
      FirstName = firstName; 
     } 
    } 

用於消費WCF服務

public partial class AgentQueue : UserControl 
    { 
     ChatACDService.ChatACDServiceClient ChatAcdClient = new ChatACDService.ChatACDServiceClient(); 
     public ObservableCollection<GetAllAgents> _GetAllAgents = new ObservableCollection<GetAllAgents>(); 

     public AgentQueue() 
     { 
      InitializeComponent(); 
      LoadAgentList(); 
      this.AllList.DataContext = _GetAllAgents; 

     } 


     private void LoadAgentList() 
     { 
      ChatAcdClient.GetAllAgentListCompleted += new EventHandler<GetAllAgentListCompletedEventArgs>(ChatAcdClient_GetAllAgentListCompleted); 
      ChatAcdClient.GetAllAgentListAsync(); 


     } 
     void ChatAcdClient_GetAllAgentListCompleted(object sender, GetAllAgentListCompletedEventArgs e) 
     { 
      if (e.Error != null) 
      { 

      } 
      else 
      { 
       // AllAgents.ItemsSource = e.Result; 
       _GetAllAgents.Add(new GetAllAgents(e.Result.ToList())); 
      } 
     } 

我用下面的代碼創建列表框XAML頁面下面的方法

<ListBox x:Name="AllList" ItemsSource="{Binding}" 
      DisplayMemberPath="FirstName" 
     Margin="403,54,0,35" HorizontalAlignment="Left" Width="101" /> 

但是輸出像,

enter image description here

我需要顯示WCF方法的結果(返回類型列表)在列表框中使用ObservableCollection.What是變化需要在上面的程序?

+1

顯示如何設置DataContext。 – Maximus

+0

@Maximus this.AllList.DataContext = _GetAllAgents; AllList是Listbox的名稱 –

+1

你期望什麼? FirstName返回的對象就是您在輸出中看到的列表。你期望得到什麼結果? – Nitram

回答

0

其實它工作得很好:

你問到你的顯示對象GetAllAgents的會員路徑「名字」。 但成員路徑「FirstName」是一個字符串列表。

因此,您的XAML顯示您期望的結果:成員路徑的toString()轉換。

而且您的會員路徑的默認的toString這名字是串名單是:System.Collection.Generic.List [System.String]

我猜你所期望的是,你的第一個列表名稱應該是ListBox的項目源。

所以,如果你唯一需要的就是展示自己的名字,只需更換

public ObservableCollection<GetAllAgents> _GetAllAgents = new ObservableCollection<GetAllAgents>(); 

通過

public ObservableCollection<string> _GetAllAgents = new ObservableCollection<string>(); 

_GetAllAgents.Add(new GetAllAgents(e.Result.ToList())); 

通過

foreach (var agentName in e.Result.ToList()) 
{ 
_GetAllAgents.Add(agentName); 
} 

它會顯示您的代理的名稱。

如果你需要的是mor,那麼你需要爲每個代理對象和一個dataTemplate創建一個viewModel,讓你知道Silverlight的顯示方式。

希望它有幫助。

+0

對於silverlight應用程序而言,我是一個新手。我如何顯示Listbox中的所有列表值而不是system.collections.generic。list1 system.string –

+0

看到更新我剛剛做了 – Ouarzy

+0

列表框顯示爲空現在 –