c# - 如何根据特定条件对DataGrid 中的数据进行分组?

标签 c# .net wpf mvvm wpfdatagrid

如果他是InProgress,我如何在GridView中为State创建组,而其他所有选项都没有任何组?

public class RecordVm: VmBase
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public State State { get; set; }
        public bool IsCompeleted { get; set; }
    }
    public enum State
    {
        Empty, Opened, InProgress, Completed
    }

     public class MainVm : VmBase
    {
        public ObservableCollection<RecordVm> RecordVms { get; } = new ObservableCollection<RecordVm>();

        public ListCollectionView ListCollection {get;}

        public MainVm()
        {
            ListCollection = new ListCollectionView(RecordVms);
            ListCollection.GroupDescriptions?.Add(new PropertyGroupDescription("State"));
        }
    }

目前,我已经为每个 State 变体创建了一个组,但这样的选择不适合我。

<DataGrid ItemsSource="{Binding ListCollection}"
      Style="{StaticResource AzureDataGrid}"
      RowStyle="{DynamicResource DataGridRowStyleStateGreen}">
<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Path=Name}" />
                                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=ItemCount}"/>
                                        <TextBlock Text=" Items"/>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

enter code here

最佳答案

如果我理解正确,这可能会按照您希望的方式进行分组: Xaml 保持不变!

型号

public class RecordVm 
        {
            public int Id { get; set; }
            public string Description { get; set; }
            public State State {
                get { return this._state; }
                set { this._state = value;
                    if (value == State.InProgress)
                        this.InProgress = true;return;
                    this.InProgress = false; }
            }
            private State _state;
            public bool IsCompeleted { get; set; }

            public bool InProgress { get; private set; }


        }

转换器

public class DisplayConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null) return "";
                if ((bool) value) return "In Progress";
                return "Finished";
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

用法

ListCollection.GroupDescriptions?.Add(new PropertyGroupDescription("InProgress", new DisplayConverter()));

关于c# - 如何根据特定条件对DataGrid 中的数据进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410779/

相关文章:

C# 回退值

c# - 没有 ASP.NET Identity 的 OWIN cookie 身份验证

.net - 使用 Microsoft graph api sdk 订阅用户状态

.net - 如何将Scala源代码编译成.Net

c# - 如何使Windows窗体应用程序响应式?

c# - 如何从文件路径中删除版本号? -Winforms C#

c# - 左/右连接与查询中的多个连接

c# - C# 泛型如何影响具有原语的集合

c# - ListView 的 ItemTemplate 中使用的 AvalonEdit 中的连接命令不起作用

c# - WPF DataTrigger 仅工作 1 次