A very simple WPF datagrid bound to a list of images. On resizing the columns using the divider (or the main window itself) the CPU goes to 100% and the app enters an infinite loop of arrange / resize resulting in the window contents jumping back and forth and no more user input handled (locked).
The datagrid is defined as:
<DataGrid ItemsSource="{Binding ImageList}" AutoGenerateColumns="False" x:Name="ImageGrid">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Image 1" Width="40*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding ImagePath}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="EmptyText" />
</DataGrid.Columns>
</DataGrid>
Things I've tried:
What I can't do:
Is there any way around this behaviour in .Net 4.0? If not, I'll have to consider a workaround such as using something else in place of the grid although I've spent ages styling it and would hate to lose that work.
Thanks in advance
I was facing the same issue, and I finally found a workaround after hours of struggling.
Add an event handler to the SizeChanged event in the DataGrid :
<DataGrid SizeChanged="DataGrid_SizeChanged">
And this is the code of the event handler :
private void DataGrid_SizeChanged(object a_sender, SizeChangedEventArgs a_args)
{
try
{
DataGrid dataGrid = a_sender as DataGrid;
if (dataGrid != null)
{
double widthDifference = Math.Abs(a_args.PreviousSize.Width - a_args.NewSize.Width);
double heightDifference = Math.Abs(a_args.PreviousSize.Height - a_args.NewSize.Height);
if ((widthDifference != 0 && widthDifference < 1) || (heightDifference != 0 && heightDifference < 1))
{
dataGrid.Measure(new Size(Math.Round(dataGrid.ActualWidth), Math.Round(dataGrid.ActualHeight)));
dataGrid.InvalidateMeasure();
}
}
}
catch (Exception) { }
}
I don't know if this is a good solution, but this is the only thing that worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With