Reputation: 4480
My understanding of DataBinding is still at the "working on it" level, so here is my issue. I have this data:
private class User
{
public string username { get; set; }
public string real_name { get; set; }
}
ObservableCollection<User> users = new ObservableCollection<User>();
...adds stuff...
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(users);
I want this to be displayed in a two column ListBox. I've gotten in in a two column ComboBox by doing:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="114,23,0,0" Name="comboBox_client" VerticalAlignment="Top" Width="113" IsEditable="True" ItemsSource="{Binding}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding username}" Name="left" Width="50" />
<TextBlock Text="{Binding real_name}" Name="right" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
with
comboBox_client.ItemsSource = view;
But I'm not sure how to make the step over to a ListBox, since I see no ItemTemplate, and I don't understand the concept behind what the above Xaml is actually doing. If I take out the ItemTemplate part and try the rest on the ListBox, I just just a listbox full of System.Windows.DataTemplate.
Point in the right direction please?
Upvotes: 0
Views: 308
Reputation: 37760
ListBox:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding username}" Width="50" />
<TextBlock Text="{Binding real_name}" Width="100" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But it seems to me, ListView is more suitable for this task:
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="User name" DisplayMemberBinding="{Binding username}" Width="50" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding RealName}" Width="100" />
</GridView>
</ListView.View>
</ListView>
Upvotes: 1
Reputation: 1995
ListBox has an ItemTemplate
property too. I think you just missed that.
You can just use the same DataTemplate
you used for your ComboBox
.
Upvotes: 3