首页 > 解决方案 > XAML 中的 ListView 和 GroupStyle

问题描述

我有一个 WPF 应用程序并创建一个静态网格表。第一列中有一个下拉单元格,单击时会显示行和列。有没有一种方法可以用 XAML 或任何其他方式进行编码?我会上传一个样本。

在此处输入图像描述

何时显示下拉详细信息。

*** 这是我的 C# 代码 ***

 public partial class ProcessData : UserControl
    {
        public ProcessData()
        {
            InitializeComponent();

            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Type = Group.One });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Type = Group.Two });
            items.Add(new User() { Name = "Sammy Doe", Age = 13, Type = Group.One });
            items.Add(new User() { Name = "testing", Age = 13, Type = Group.Three });
            lvUsers.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Type");
            view.GroupDescriptions.Add(groupDescription);
        }
        public enum Group { One, Two, Three };

        public class User
        {
            public string Name { get; set; }

            public int Age { get; set; }

            public string Mail { get; set; }

            public Group Type { get; set; }
        }
    }

*** 这是我现在的 XAML 代码 ***

<ListView Name="lvUsers">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                    </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="11" VerticalAlignment="Bottom" />
                                                        <TextBlock Text="{Binding ItemCount}" FontSize="11" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                                        <TextBlock Text=" item(s)" FontSize="11" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>

在我的代码中,所有项目都分组。我想要的是如果某些数据不在类型中,它将在组之外并且不属于任何组。

标签: c#wpfxaml

解决方案


推荐阅读