动态添加组说明

本文关键字:说明 添加 动态 | 更新日期: 2023-09-27 17:56:17

我正在寻找一种动态添加组描述的方法,特别是在视图模型中,我实际上在代码后面添加了组描述,如下所示:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<Country> items = new List<Country>();
        items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie A" } }); // Added "new League"
        items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie B" } });
        items.Add(new Country() { Name = "England", League = new League { Name = "Premiere League" } });
        items.Add(new Country() { Name = "Spain", League = new League { Name = "Primeira Division" } });
        lvUsers.ItemsSource = items;
        CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
        PropertyGroupDescription groupNations = new PropertyGroupDescription("Name");
        view.GroupDescriptions.Add(groupNations);
        PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League.Name"); // Changed "League" to "League.Name"
        view.GroupDescriptions.Add(groupCompetions); // Fixed the variable name here
        lvUsers.ItemsSource = view; 
    }
}
public struct League
{
    public string Name { get; set; }
}
public struct Country
{
    public string Name { get; set; }
    public League League { get; set; } // added getter and setter
}

这是我的 XAML:

<Window x:Class="GroupBox_Header.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:GroupBox_Header"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="10">
    <ListView Name="lvUsers">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Home Team" Width="50" DisplayMemberBinding="{Binding HomeTeam}" />
                <GridViewColumn Header="Away Team" Width="50" DisplayMemberBinding="{Binding AwayTeam}" />
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                                <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                                <TextBlock Text=" Items" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding League}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</Grid>

我想做的是在列表视图中绑定一个自动具有组描述的集合。

动态添加组说明

如果我理解正确的问题,您可以直接从ViewModel,从代码隐藏中控制GroupDescription,或者创建一个自定义ListView,使您可以绑定到ObservableCollection。

视图模型

使用 CollectionViewSource 从 ViewModel 获取 CollectionView。你可以争辩说这是一种反模式,但我认为没关系。

代码隐藏

代码中创建一个依赖项属性,该属性在更改时将更新视图的组描述符。然后在 xaml 中将该属性绑定到视图模型。

自定义列表视图

从 ListView 继承并创建一个依赖项属性,该属性允许您绑定到 ObservableCollection。

无论哪种方式,您都需要控制不同的场景,例如属性已更改,集合已更改。