Columns are collapsed when using group in datagrid

2020-05-20 00:24发布

问题:

I have a datagrid in which I display several objects with some columns. I added grouping to the datagrid and now it seems that it doens't like the colulmns with width of '*' - the columns are all collapsed to their minimum widths. However, when I refresh the grid (after adding an element or modifiying an existing one), I can see the columns refreshing alright. The weird thing is that I call the same function twice and the first time it doesn't work while the second it does.

Before :

http://www.microage-dil.ca/SO/GridBefore.png

After updating once

http://www.microage-dil.ca/SO/GridAfter.png Here is the Refresh function :

Private Sub ListerDocuments()
    Dim lstCVDocuments As New ListCollectionView(_oLstDocuments)
    lstCVDocuments.GroupDescriptions.Add(New PropertyGroupDescription("TypeFichier"))

    GridDocuments.ItemsSource = lstCVDocuments
    GridDocuments.Items.Refresh()
End Sub

And now the datagrid :

<DataGrid x:Name="GridDocuments" Grid.Column="0" ItemsSource="{Binding}" Style="{StaticResource BaseGrid}"  IsTabStop="False">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                        <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"  HorizontalAlignment="Stretch">
                                            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
    <DataGrid.Columns>                            
        <DataGridTextColumn Header="Nom" Width="2*"  MinWidth="150"
                            Binding="{Binding NomFichier}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Fichier" Width="3*"  MinWidth="150"
                            Binding="{Binding NomFichierOriginal}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Extension" Width="65"  MinWidth="50"
                            Binding="{Binding ExtensionFormate}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Date d'ajout" Width="80"  MinWidth="80"
                            Binding="{Binding  DateAjout, StringFormat=yyyy-MM-dd}"                                               
                            IsReadOnly="True" />
    </DataGrid.Columns>                        
</DataGrid>

I got the grouping part on the net and it seemed to work fine until I found tha tparticular bug. Note that I added the HorizontalAlignment ="Strech" because I though it might solve the problem but obviusly it did not.

EDIT : Forgot to mention, the first time I call the ListerDocument function, the variable is fully loaded with the documents and it is called in the constructor (New)

回答1:

Try to specify the GroupStyle.Panel in your DataGrid. The reason for this is the fact that the default GroupStyle.Panel (StackPanel) does not fit for the DataGrid Width "*" and the columns will collapse.

<GroupStyle.Panel>
  <ItemsPanelTemplate>
    <DataGridRowsPresenter/>
  </ItemsPanelTemplate>
</GroupStyle.Panel>