![](/img/trans.png)
[英]Update ItemsControl.ItemsSource based on selected object of a another ItemsControl
[英]WPF ItemsControl: Change ItemsSource of ItemsControl from Current Selection of another ItemsControl
我有一个包含产品类别的ItemsControl,我还有另一个ItemsControl,其中包含当前选择的所有商品的列表,我需要将当前的类别选择与ItemsControl商品的绑定相关联
<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>
每当Category
更改时,您只需要更新数据绑定的Article
集合。 如果使用ListBox.SelectedItem
将属性添加到数据绑定中,则可以从属性设置器中执行以下操作:
<ListBox ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory}" ... />
..
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.