首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF ItemsControl:将ItemsControl的ItemsSource从当前选择的另一个ItemsControl更改为

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

Stack Overflow用户
提问于 2014-07-03 16:10:48
回答 1查看 966关注 0票数 0

我有一个包含产品类别的ItemsControl,还有一个包含当前选择的所有文章的列表的ItemsControl,我需要将当前选择的类别与ItemsControl文章的绑定关联起来

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2014-07-03 16:20:30

您只需在每次更改Category时更新数据绑定Article集合。如果使用ListBox.SelectedItem将属性添加到数据绑定中,则可以通过属性设置器执行此操作:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Categories}"
    SelectedItem="{Binding SelectedCategory}" ... />

。。

代码语言:javascript
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24548336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档