Columns are reset when using a group in a datagrid

I have a datagrid in which I show several objects with some columns. I added grouping to the datagrid, and now it seems that it does not look like colulmns with a width of '*' - all columns are collapsed to the minimum width. However, when I update the grid (after adding an element or modifying an existing one), I can see that the columns are updating normally. The strange thing is that I call the same function twice, and the first time it does not work, and the second it does.

Before:

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

After update after

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 on the network, and it seemed to work fine until I found the tarp error. Please note that I added HorizontalAlignment = "Strech", because although this may solve the problem, but obviusly it is not.

EDIT: Forgetting the mention, when I first call the ListerDocument function, the variable is fully loaded by the documents and called in the constructor (New)

+8
wpf datagrid grouping
source share
1 answer

Try specifying GroupStyle.Panel in your DataGrid. The reason for this is the fact that, by default, GroupStyle.Panel (StackPanel) is not suitable for the width of the DataGrid "*", and the columns are crashing.

 <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> 
+24
source share

All Articles