C# WPF ItemsControl:从当前选择的另一个ItemsControl更改ItemsControl的ItemsSource
我有一个包含产品类别的ItemsControl,还有一个ItemsControl,其中包含当前选定的所有类别文章的列表,我需要将当前选定的类别与ItemsControl文章的绑定关联起来C# WPF ItemsControl:从当前选择的另一个ItemsControl更改ItemsControl的ItemsSource,c#,wpf,xaml,binding,itemscontrol,C#,Wpf,Xaml,Binding,Itemscontrol,我有一个包含产品类别的ItemsControl,还有一个ItemsControl,其中包含当前选定的所有类别文章的列表,我需要将当前选定的类别与ItemsControl文章的绑定关联起来 <ItemsControl ItemsSource="{Binding Path=Categories}"> ... <ItemsControl.ItemTemplate> <DataTemplate> <Button Content="{B
<ItemsControl ItemsSource="{Binding Path=Categories}">
...
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Path=CategorieCaption}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
...
</ItemsControl>
<ItemsControl ItemsSource="{Binding Path=SelectedCategories.Articles}">
...
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Path=ArticleCaption}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
...
</ItemsControl>
...
...
...
...
每次更改类别
时,您只需更新数据绑定的文章
集合。如果使用列表框向数据绑定添加属性。选择editem
,则可以从属性设置器执行此操作:
<ListBox ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory}" ... />
除了ItemsControl:-)hi@Sheridan中没有
SelectedItem
属性外,ItemsControl不能选择项目,只能显示集合然后使用列表框。再次感谢。。。没有你我怎么办,克莱门斯?我已经相应地更新了我的答案。您实际如何在ItemsControl中选择项?您或许应该使用列表框。
public Category SelectedCategory
{
get { return selectedCategory; }
set
{
if (selectedCategory != value)
{
selectedCategory = value;
NotifyPropertyChanged("SelectedCategory");
// Selected Category was changed, so update Articles collection
Articles = UpdateArticlesByCategory(selectedCategory);
}
}
}