I am new to WPF and data binding, here I have a Listbox which works just fine:
<ListBox
x:Name="listBox" ItemsSource="{Binding Path=AllFriends}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now I want to try DataContext so I use:
<UserControl.Resources>
<CollectionViewSource
x:Key="FriendsGroups"
Source="{Binding Path=AllFriends}"
>
</CollectionViewSource>
</UserControl.Resources>
<ListBox
x:Name="listBox"
DataContext="{StaticResource FriendsGroups}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But this one is not working. I would appreciate if you could tell me why?
答案 0 :(得分:1)
即使您使用CollectionViewSource
,仍然需要设置ItemsSource
属性,而不是DataContext
:
<ListBox ItemsSource="{Binding Source={StaticResource FriendsGroups}}">