WPF ItemsControl:从另一个ItemsControl的当前选择更改ItemsControl的ItemsSource

时间:2014-07-03 08:10:48

标签: c# wpf xaml binding itemscontrol

我有一个包含产品类别的ItemsControl,我有另一个ItemsControl,其中包含catégoie当前选中的所有文章的列表,我需要将该类别的当前选择与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>

1 个答案:

答案 0 :(得分:0)

每次更改Article时,您只需更新数据绑定Category集合。如果使用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);
        }
    }
}