Reputation: 21
I have a WPF DataGrid (from the WPFToolkit package) like the following 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 be automatically adjusted such that all three columns fill the width of the grid, so I set Width="1*"
on every column. I encountered two problems with this approach.
ItemsSource
of the DataGrid is null
or an empty List, the columns won't size to fit the width of the grid but have a fixed width of about 20 pixel. Please see the following picture: http://img169.imageshack.us/img169/3139/initialcolumnwidth.png
http://img88.imageshack.us/img88/9362/columnwidthaftermaximiz.png
I was able to solve problem #3 by deriving a sub class from DataGrid and override the DataGrid's OnRenderSizeChanged
method 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?
Upvotes: 2
Views: 4866
Reputation: 71
All DataGridTextColumn has a default MinWidth prperty set to 20. So, if you want to control width after resizing from max to min you have to specify this value yourself. My proposal is:
<DataGridTextColumn Header="Perfect
column" Binding="{Binding SomeMember}" MinWidth="120" Width="120*"/>
Upvotes: 0
Reputation: 21
I have exactly the same problem as you but instead of listening to RenderSizeChanged, I listened to the SizeChanged event and it accounts for 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;
}
};
Upvotes: 2
Reputation: 66445
The following override fixes the size problem for the width growing bigger including maximize. But still not the restore size problem.
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 arose for me when I defined a GroupStyle template for the DataGrid, so it seems like a bug.
Upvotes: 0
Reputation: 1
I have also faced the same problem as you stated. I searched a bit in the internet, and probably find a solution. The solution actually works for me properly. Set the * value from C# code. Here is the code snippet,
dg.Coulmns[0].Width = new DataGridLength(1.0, DatGridLengthUnitType.Star);
It will set the 0th column's width to *. Don't forget to remove the Width="1**" from the XAML page.
Enjoy Guru.
Upvotes: 0