Databinding driving me crazy!
-
Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:
private void PopulateContactsListBox()
{
listBoxContacts.Items.Clear();
listBoxContacts.ItemsSource = contactClient.GetContacts();
}My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!
-
Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:
private void PopulateContactsListBox()
{
listBoxContacts.Items.Clear();
listBoxContacts.ItemsSource = contactClient.GetContacts();
}My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!
I'm not sure if this is the same in WPF, but in ASP.NET the databound controls have properties for you to set which property or field of the returned data items are used for 1) displaying text and 2) the actual value of the item. Perhaps WPF has something similar?
-
Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:
private void PopulateContactsListBox()
{
listBoxContacts.Items.Clear();
listBoxContacts.ItemsSource = contactClient.GetContacts();
}My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!
You need to set the items that you want to display. Typically it would look something like this:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=Surname}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>Deja View - the feeling that you've seen this post before.
-
You need to set the items that you want to display. Typically it would look something like this:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=Surname}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>Deja View - the feeling that you've seen this post before.
Yeah, as Pete indicated you need a datatemplate as the listbox doesn't know how to display a Friend object so it just calls the ToString method. You can move the datatemplate into a resource section. Such as Window.Resources or even Application.Resources. Another neat thing is if you set the DataType on the DataTemplate to your Friend object, it will be used automatically display it. Along the lines of this: ....
-
Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:
private void PopulateContactsListBox()
{
listBoxContacts.Items.Clear();
listBoxContacts.ItemsSource = contactClient.GetContacts();
}My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!
-
You're welcome, and you have a nice day too. :-D
Deja View - the feeling that you've seen this post before.