【问题标题】:GridView Filter buttons for collection用于集合的 GridView 筛选按钮
【发布时间】:2015-03-31 06:34:15
【问题描述】:

我目前在集合视图上进行数据绑定以显示我的所有按钮。

但是,由于我的收藏列表中有超过 24 项,我想在同一页面上添加四个按钮(开胃菜、早餐、午餐、晚餐),并根据我单击的一个进行过滤。这样我一次只显示其中的几个选项。

我应该在 ViewModel 中处理这个问题吗?或者这应该是网页浏览的责任吗?

MenuItem 类:

public enum type
{
    Appetizer,
    Breakfast,
    Lunch,
    Dinner
};
class MenuItem
{
 //stuff
}

菜单项页面:

    public sealed partial class MenuItemsPage : Page
    {
        public MenuItemsPage()
        {
            this.InitializeComponent();       
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string tableName = e.Parameter as string;
            this.DataContext = new MenuPageVM(tableName);  //this returns List<MenuItem> 24 items. All 4 types of MenuItems    
        }
    }
}

XAML:

<CollectionViewSource x:Key="MenuItemsCollectionViewSource" Source="{Binding MenuItems}"/>
<GridView   SelectionMode="None" 
            x:Name="menuGrid" ItemsSource="{Binding Mode=OneTime, Source={StaticResource MenuItemsCollectionViewSource}}" Margin="256,113,320,184"
                    ItemTemplate="{StaticResource MenuButtonTemplate}"/>

【问题讨论】:

    标签: c# wpf xaml gridview mvvm


    【解决方案1】:

    在视图模型中保留一个属性 AllItems。单击按钮时,可以调用视图模型中的方法进行过滤。

    public void Filter(type filterType)
    {
        MenuItems.Clear();
        foreach(var item in AllItems)
        {
           if(item.type == filterType)
           {
              MenuItems.Add(item);
           }
        }
    }
    

    仅当 MenuItems 是 ObservableCollection 时,更改才会反映在 UI 中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多