【问题标题】:How to display several sets in a grouped list in WPF?如何在 WPF 的分组列表中显示多个集合?
【发布时间】:2017-12-27 15:09:23
【问题描述】:

我想创建一个分为多个组的列表,以便可以隐藏任何组中的项目。

现在,每个组的项目都保存在一个单独的集合中,所有集合(总共 5 个)都保存在一个对象中。但是我可以修改它们的存储方式,如果它会使代码更简单。

【问题讨论】:

    标签: c# wpf tracker


    【解决方案1】:

    试试这个,用 ListBox 轻松将 ListBox 的 ItemTemplate 更改为 Expander。 例如: ScreenShot XAML:

    <Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowVM/>
    </Window.DataContext>
    <Grid>
        <ListView ItemsSource="{Binding ItemsList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Expander Header="{Binding Name}" >
                        <ListView ItemsSource="{Binding Items}"/>
                    </Expander>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

    视图模型:

    public class MainWindowVM
    {
        public MainWindowVM()
        {
            ItemsList = new List<Group>();
            var items  = new List<Item>();
            items.Add(new Item("Item1")); 
            items.Add(new Item("Item2")); 
            items.Add(new Item("Item3"));
            //items.Add("Item4"); 
            //items.Add("Item5");
            ItemsList.Add(new Group()
            {
                Name = "List1",
                Items = items
            });
    
            items.Add(new Item("Item4"));
            ItemsList.Add(new Group()
            {
                Name = "List2",
                Items = items
            });
            items.Add(new Item("Item5"));
            ItemsList.Add(new Group()
            {
                Name = "List3",
                Items = items
            });
        }
    
        public List<Group> ItemsList { get; set; }
    }
    
    public class Group
    {
        public string Name { get; set; }
        public List<Item> Items { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
    public class Item
    {
        public Item(string name)
        {
            Name = name;
        }
        public string Name { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
    

    【讨论】:

    • 如何在运行时更改列表?
    • 你需要实现接口INotifyPropertyChanged
    【解决方案2】:

    一旦您使用 WPF 框架,您就可以定义 ListBox.GroupStyle 以及内部项目的常规样式。然后将您的列表框绑定到 CollectionViewSource 类似的东西:

    ICollectionView view = CollectionViewSource.GetDefaultView(*your-collection-here*);
    view.GroupDescriptions.Add(new PropertyGroupDescription(*your-grouping-field-here*));
    view.SortDescriptions.Add(new SortDescription((*your-grouping-field-here*, ListSortDirection.Ascending));
    your-listbox-here.ItemsSource = view;
    

    所有学分都在这里:http://www.c-sharpcorner.com/uploadfile/dpatra/grouping-and-sorting-in-listbox-in-wpf

    要展开/折叠组,您可以添加一个保持当前状态(折叠或展开)的 bool 属性,并在您的样式中考虑到这一点(比如说,使其仅对展开的项目可见,更改“展开/折叠”雪佛龙图标等)

    【讨论】:

    • 是否可以使用类似于 Андрій Петрук 的答案的“扩展器”,而不是 bool 属性?如果是这样 - 如何?
    • @Jecke,您必须在“组样式”中添加一个按钮。该按钮的目的是切换视图模型中的 bool 属性。然后,组样式可以自动刷新其按钮图片(从“向下箭头”到“向上箭头”),并且项目样式可以整体改变其可见性(类似于 Visibility={Binding Path=Expanded, Converter={StaticResource BoolToVisibilityConverter}} )
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2014-11-26
    相关资源
    最近更新 更多