Search code examples
c#.netwpfmvvmwpfdatagrid

How to group the data in a DataGrid on a certain condition?


How do I get in the GridView to create groups for State if he is of InProgress, and all other options have been without any group?

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"));
        }
    }

At the moment, I have created a group for each of the variants of State, but such an option does not suit me.

<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

Solution

  • If i understood you correctly, this might group the way you want it to: Xaml remains Untouched!

    Model

    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; }
    
    
            }
    

    Converter

    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();
                }
            }
    

    Usage

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