Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF ItemsControl:从当前选择的另一个ItemsControl更改ItemsControl的ItemsSource_C#_Wpf_Xaml_Binding_Itemscontrol - Fatal编程技术网

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

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,还有一个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>

...
...
...
...

每次更改
类别
时,您只需更新数据绑定的
文章
集合。如果使用
列表框向数据绑定添加属性。选择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);
        }
    }
}