簡體   English   中英

WPF ItemsControl:從另一個ItemsControl的當前選擇中更改ItemsControl的ItemsSource

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM