C# WCF到WPF客户端-填充列表框
这是一种从来自WCF服务的数据填充ListBox的方法C# WCF到WPF客户端-填充列表框,c#,wpf,wcf,listbox,C#,Wpf,Wcf,Listbox,这是一种从来自WCF服务的数据填充ListBox的方法 private void FillListbox() { ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); client.Open(); listBox.ItemsSource = client.GetAllProducts();
private void FillListbox()
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.Open();
listBox.ItemsSource = client.GetAllProducts();
}
但在列表框中我只能看到
ProductListClient.ServiceReference1.Product
因此,我在模型中添加了一个override ToString()方法,但仍然无法在列表框中看到数据。如何修复此问题?似乎绑定正在工作,并且您的列表中包含项目。现在,您需要在列表框中实现ItemDataTemplate,如下所示:
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
忽略我的示例的命名。您需要适应应用程序中对象的名称方法不是序列化过程的一部分,因此您的ToString覆盖不存在于客户端。您必须将该类型提取到共享的datacontract库中,或者按照Maxim所说的操作。Thanx@Maxim Fleitling