![](/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.