We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Expand or Collapse the specific Group MVVM

I have a SfDataGrid with AutoExpandGroups="False"

<syncf:SfDataGrid Grid.Row="0" Grid.Column="0"  x:Name="DataGrid" MinHeight="500"
                              ItemsSource="{Binding Lista}"
                              SelectedItem="{Binding SelectedLista}"
                              AutoGenerateColumns="False"
                              ShowGroupDropArea="False"
                              AllowGrouping="True"
                              AutoExpandGroups="False"
                              AllowFiltering="True"
                              AlternatingRowStyle="{StaticResource AlternatingRowStyle}"
                              SelectionMode="Single"                             
                              SelectionUnit="Row"
                              NavigationMode="Row"
                          >

               <syncf:SfDataGrid.GroupColumnDescriptions>
                    <syncf:GroupColumnDescription ColumnName="nombre" />
                </syncf:SfDataGrid.GroupColumnDescriptions>

               <syncf:SfDataGrid.Columns>
                    <syncf:GridTextColumn MappingName="nombre"
                                      HeaderText="Nombre"
                                      AllowGrouping="True"
                                      GroupMode="Value"
                                      Padding="3,0,3,0"
                                      AllowFiltering="True"
                                      AllowBlankFilters="True" />
                    <syncf:GridTextColumn MappingName="identificador"
                                      HeaderText="Identificador"
                                      Padding="3,0,3,0" />
                    <syncf:GridTextColumn MappingName="nombre"
                                      HeaderText="Programa"
                                      Padding="3,0,3,0" />
     </syncf:SfDataGrid>

I'm using MVVM and I need to Expand a specifc group with MVVM.

Something like this but in MVVM.
var group = (dataGrid.View.Groups[0] as Group);
this.dataGrid.ExpandGroup(group);

this.dataGrid.CollapseGroup(group);

3 Replies

MK Muthukumar Kalyanasundaram Syncfusion Team April 10, 2017 01:08 PM UTC

Hi Salva, 
 
Thank you for contacting Syncfusion support. 
 
We have analyzed your query. If you want to expand and collapse a specific group of record with MVVM pattern, you can achieve your requirement as shown like below code, 
 
Code Snippet: 
 
public static class Commands 
{ 
    static Commands() 
    { 
        CommandManager.RegisterClassCommandBinding(typeof(SfDataGrid), new CommandBinding(ExpandGroup, OnExpandGroup, OnCanExpandCollapse)); 
        CommandManager.RegisterClassCommandBinding(typeof(SfDataGrid), new CommandBinding(CollapseGroup, OnCollapseGroup, OnCanExpandCollapse)); 
    }            
 
    public static RoutedCommand ExpandGroup = new RoutedCommand("ExpandGroup", typeof(SfDataGrid)); 
    public static RoutedCommand CollapseGroup = new RoutedCommand("CollapseGroup", typeof(SfDataGrid)); 
 
    private static void OnExpandGroup(object sender, ExecutedRoutedEventArgs args) 
    { 
        var dataGrid = args.Source as SfDataGrid; 
        var group = (dataGrid.View.Groups[0] as Group); 
        dataGrid.ExpandGroup(group); 
    } 
 
    private static void OnCollapseGroup(object sender, ExecutedRoutedEventArgs args) 
    { 
        var dataGrid = args.Source as SfDataGrid; 
        var group = (dataGrid.View.Groups[0] as Group); 
        dataGrid.CollapseGroup(group); 
    } 
    private static void OnCanExpandCollapse(object sender, CanExecuteRoutedEventArgs e) 
    { 
        e.CanExecute = true; 
    } 
} 
 
 
Please let us know if you have any query. 
 
Regards, 
Muthukumar K 



SA Salva April 12, 2017 03:01 PM UTC

Thanks, In my case, this not solve totally the problem but it have helped to solve it.

To solve it I have used

Command="{Binding ModifyCommand}"
CommandParameter="{Binding ., ElementName=DataGrid}"

And I have all the datagrid in my function.




MK Muthukumar Kalyanasundaram Syncfusion Team April 13, 2017 08:38 AM UTC

Hi Salva,   
 
Thanks for the update. 
 
We are glad that you have fixed your issue. Please let us know if you need any other assistance.   
 
Regards,  
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon