Resizing WPF DataGrid window does not resize DataGridColumns
I have a WPF DataGrid (from WPFToolkit package) like in my application.
<Controls:DataGrid>
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column1}" Header="Column 1" />
<Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column2}" Header="Column 2" />
<Controls:DataGridTextColumn Width="1*" Binding="{Binding Path=Column3}" Header="Column 3" />
</Controls:DataGrid.Columns>
</Controls:DataGrid>
The column width should automatically adjust so that all three columns fill the width of the grid, so I set Width="1*"
for each column. I ran into two problems with this approach.
- When the
ItemsSource
DataGrid isnull
either a value or an empty list, the columns will not be sized to match the width of the grid, but have a fixed width of about 20 pixels. Please view the following image:http://img169.imageshack.us/img169/3139/initialcolumnwidth.png
- When I maximize the application window, the columns will not resize but keep their original size. See the following image:
http://img88.imageshack.us/img88/9362/columnwidthaftermaximiz.png
- When I resize the application window with the mouse, the columns will not resize.
I managed to solve problem # 3 by subclassing the DataGrid and overriding the DataGrid method OnRenderSizeChanged
as follows.
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
foreach (var column in Columns)
{
var tmp = column.GetValue(DataGridColumn.WidthProperty);
column.ClearValue(DataGridColumn.WidthProperty);
column.SetValue(DataGridColumn.WidthProperty, tmp);
}
}
Unfortunately, this does not solve problems # 1 and # 2. How can I get rid of them?
a source to share
I have exactly the same problem as you, but instead of listening to RenderSizeChanged, I listened to the SizeChanged event and considered all of your situations.
dataGrid.SizeChanged += (sender_, arg_) =>
{
foreach (var dataGridColumn in dataGrid.Columns)
{
DataGridLength dataGridLength = dataGridColumn.Width;
dataGridColumn.ClearValue(DataGridColumn.WidthProperty);
dataGridColumn.Width = dataGridLength;
}
};
a source to share
I also ran into the same problem you mentioned. I searched the internet a bit and probably found a solution. The solution actually works for me correctly. Set the value * from C # code. Here is a code snippet,
dg.Coulmns [0] .Width = new DataGridLength (1.0, DatGridLengthUnitType.Star);
It will set the width of the 0th column to *. Remember to remove Width = "1 **" from the XAML page.
Enjoy the Guru.
a source to share
The following override fixes the sizing issue for increasing width, including maximizing. But the problem with the size of the recovery has not yet been resolved.
protected override void OnChildDesiredSizeChanged(UIElement e)
{
base.OnChildDesiredSizeChanged(e);
if (e.FindChild<DataGrid>() != null)
{
foreach (var column in dataGridMain.Columns)
{
var tmp = column.GetValue(DataGridColumn.WidthProperty);
column.ClearValue(DataGridColumn.WidthProperty);
column.SetValue(DataGridColumn.WidthProperty, tmp);
}
}
}
PS this problem only came up for me when I defined the GroupStyle template for the DataGrid, so it seems to be an error.
a source to share