Search code examples
c#wpfdatagrid

wpf datagrid expand all not working


Hi have a Datagrid with groups, i added a button to Expand All groups but it's not working, all groups stay collapsed.

I'm using PropertyChanged event Handler and a button with a Command

Here is the xaml:

        <StackPanel Grid.Row="0">
        <Button x:Name="ExpandAll"  Content="Tout deplier" VerticalAlignment="Bottom" Command="{Binding ExpandAll}"/>
        <!-- This textblock text is updated by the Expanded property changed -->
        <TextBlock Text="{Binding Expanded}" />
    </StackPanel>
    <DataGrid x:Name="GrdLignes" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0,0,0,0"
              Grid.Row="1" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserAddRows="False"
              CanUserDeleteRows="False" ItemsSource="{Binding Lignes}" IsReadOnly="True"
              RowDetailsVisibilityMode="VisibleWhenSelected" RowHeaderWidth="25">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Background="Lavender" IsExpanded="{Binding Expanded}">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Path=Name}" Padding="0,0,5,0" FontWeight="Bold" />
                                                <TextBlock Text="{Binding Path=ItemCount}" Padding="0,0,5,0"/>
                                                <TextBlock Text="Commandes"/>
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Pièce Achat" Binding="{Binding Path=Piece}" FontWeight="Bold"/>
            <DataGridTextColumn Header="Type" Binding="{Binding Path=TypeLabel}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="{Binding Path=Type, Converter={StaticResource TypeToBrushConverter}}" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Statut" Binding="{Binding Path=StatutLabel}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="{Binding Path=Statut, Converter={StaticResource StatutToBrushConverter}}" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid RowHeaderWidth="25" ItemsSource="{Binding Path=Lignes}" AutoGenerateColumns="False" Margin="0" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Acheteur" Binding="{Binding Path=Acheteur}"/>
                        <DataGridTextColumn Header="Pièce" Binding="{Binding Path=Piece}"/>
                        <DataGridTextColumn Header="Client" Binding="{Binding Path=Client}"/>
                        <DataGridTextColumn Header="Ref" Binding="{Binding Path=ArRef}"/>
                        <DataGridTextColumn Header="Ref Fourn" Binding="{Binding Path=RefFourn}"/>
                        <DataGridTextColumn Header="Designation" Binding="{Binding Path=Designation}"/>
                        <DataGridTextColumn Header="Qte" Binding="{Binding Path=CmQte}"/>
                        <DataGridTextColumn Header="Vendeur" Binding="{Binding Path=Vendeur}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

Here is the viewModel:

public class MainViewModel : INotifyPropertyChanged
{
    private bool _expanded = false;

    public bool Expanded
    {
        get { return _expanded; }
        set
        {
            _expanded = value;
            OnPropertyChanged("Expanded");
        }
    }

    public ICommand ExpandAll { get; set; }

    public MainViewModel()
    {
        ExpandAll = new Command(ExpandAllAction);
    }

    private void ExpandAllAction(object parameters)
    {
        Expanded = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Solution

  • I found a solution from this thread: https://stackoverflow.com/a/12611779/3182178

    I added in MainWindow class this code:

        public static class VisualTreeHelper
        {
            public static Collection<T> GetVisualChildren<T>(DependencyObject current) where T : DependencyObject
            {
                if (current == null)
                    return null;
    
                var children = new Collection<T>();
                GetVisualChildren(current, children);
                return children;
            }
            private static void GetVisualChildren<T>(DependencyObject current, Collection<T> children) where T : DependencyObject
            {
                if (current != null)
                {
                    if (current.GetType() == typeof(T))
                        children.Add((T)current);
    
                    for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(current); i++)
                    {
                        GetVisualChildren(System.Windows.Media.VisualTreeHelper.GetChild(current, i), children);
                    }
                }
            }
        }
    
        private void ExpandAll_OnClick(object sender, RoutedEventArgs e)
        {
            Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(GrdLignes);
    
            foreach (Expander expander in collection)
                expander.IsExpanded = true;
        }
    
        private void CollapseAll_OnClick(object sender, RoutedEventArgs e)
        {
            Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(GrdLignes);
    
            foreach (Expander expander in collection)
                expander.IsExpanded = false;
        }
    

    Then inside the xaml i added two button with this code:

    <Button Name="ExpandAll"  Content="++" VerticalAlignment="Bottom" Width="30" Click="ExpandAll_OnClick"/>
    <Button Name="CollapseAll"  Content="--" VerticalAlignment="Bottom" Width="30" Margin="0" Click="CollapseAll_OnClick"/>
    

    It's not the best but it's working...