DataGrid-Column widths not updated when ItemsSource changes

XAML:

<DataGrid.Columns> <DataGridTextColumn x:Name="colDisplayName" Width="Auto" IsReadOnly="True" Header="Name" Binding="{Binding ssn.SSN_DISPLAY_NAME}"></DataGridTextColumn> <DataGridTextColumn x:Name="colValue" Width="Auto" Header="Value" Binding="{Binding ssv.SSV_VALUE}" CellStyle="{StaticResource SingleClickEditing}"></DataGridTextColumn> <DataGridTextColumn x:Name="colDescription" Width="Auto" IsReadOnly="True" Header="Description" Binding="{Binding ssn.SSN_DESCRIPTION}"></DataGridTextColumn> <DataGridTextColumn x:Name="colUnit" Width="Auto" IsReadOnly="True" Header="Unit Abbreviation" Binding="{Binding ssn.UNIT_TYPE.UNIT_NAME.UN_ABBREVIATION}"></DataGridTextColumn> </DataGrid.Columns> 

CS:

 private void tvSystemConfiguration_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { dgSystemSettings.ItemsSource = ((tvSystemConfiguration.SelectedItem as SYSTEM_SETTINGS_GROUP).SYSTEM_SETTINGS_NAMEs.Join (ssdc.SYSTEM_SETTINGS_VALUEs, x => x.SSN_ID, y => y.SSV_SSN_ID, (x, y) => new DataGridItem{ ssn = x, ssv = y })).ToList(); } 

Column widths are compressed to fit the requirements when increasing the size of the column, but if the column size is correctly reduced, they do not fit properly. It will not reduce the size of the column in the ItemsSource update after it is expanded. Hope this makes sense. Any help is appreciated. Thanks.

+4
source share
4 answers

didn't make any sense to me, sorry.

I understand the code, but this part:

Column widths are compressed for proper installation when increasing the size of the column, but do not clean properly when reducing the size of the column. I will not reduce the size of the column in updating the ItemsSource after it has increased

puzzles me

I understood it like this:

if the width of the contents of the column increases, the width of the column increases, but if the width of the contents decreases, the width of the column is absent.

Is it correct?

If so, this is normal: Wpf will just resize the datagrid column set to Auto if necessary, i.e. content cannot be fully displayed. Therefore, when the width of the content decreases, the column does not change, because the content can still be seen completely.

the only way wpf recalculates the width of the columns would be to make them all equal to 0, and then return to the auto in the code behind, with one or two updateLayout () that were introduced, but this is not very nice programming: - /

Edit: basically, in your code:

 foreach (DataGridColumn c in dg.Columns) c.Width = 0; // Update your DG source here foreach (DataGridColumn c in dg.Columns) c.Width = DataGridLength.Auto; 

and you probably need dg.UpdateLayout () or two where it is (after the update and installation return to the car, probably)

+7
source

For user convenience, here is an extension method based on David's answer:

 public static void AutoResizeColumnWidths(this System.Windows.Controls.DataGrid dataGrid) { // http://stackoverflow.com/questions/4725724/wpf-datagrid-column-widths-not-updating-on-itemssource-change foreach (var column in dataGrid.Columns) column.Width = 0; dataGrid.UpdateLayout(); foreach (var column in dataGrid.Columns) column.Width = System.Windows.Controls.DataGridLength.Auto; dataGrid.UpdateLayout(); } 

And what will it be called:

 dataGrid.AutoResizeColumnWidths(); 
+3
source

Significantly less flash on the screen and fewer lines, without calling updateLayout you need:

  foreach (DataGridColumn col in dg.Columns) { col.Width = DataGridLength.SizeToCells; col.Width = DataGridLength.Auto; } 
+1
source

Maybe I can help. I had the same problem and I found, I think, a pleasant solution.

 private void DataGrid_PreviewMouseMove(object sender, MouseEventArgs e) { DataGrid dg = (DataGrid)sender; if (e.LeftButton == MouseButtonState.Pressed) { double width = 0; foreach (DataGridColumn column in dg.Columns) { width += column.ActualWidth; } dg.Width = width + 2; } } 
0
source

Source: https://habr.com/ru/post/1336007/


All Articles