Mobile Development at U2U Consult

Based in Brussels, Belgium, U2U Consult has been offering consulting & development services in the EMEA region for over 10 years. We offer mobile development on all platforms from Apple to Android & Microsoft.

Welcome to a WPF DataGrid crash course ... literally. It will show you how Cider can give you a hangover. It took me 45 minutes, 10 process kills, and a full reboot to figure out what was going on in my application, so I gladly share my experience.

Part one: the bug

Create a WPF Form with an empty DataGrid, like this:

<Window x:Class="U2UConsult.DockOfTheBay.DataGridWorkaround"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"

Title="Bug Workaround" SizeToContent="WidthAndHeight">

<Grid>

<toolkit:DataGrid>

<toolkit:DataGrid.Columns>

</toolkit:DataGrid.Columns>

</toolkit:DataGrid>

</Grid>

</Window>

Then, add a style that adds a Margin to the Button control, e.g. as a Resource for the Window:

<!-- Kaboom-->

<Window.Resources>

<Style TargetType="{x:Type Button}">

<Setter Property="Margin" Value="3" />

</Style>

</Window.Resources>

Now is a good time to save your solution.

Add a column to the DataGrid:

<toolkit:DataGridTextColumn Header="Test" />

You will probably see something like this now, with an ever increasing counter:

What you just experienced is a confirmed bug. The current version of the WPF DataGrid hangs Visual Studio.NET's Designer when buttons are decorated with a margin.

Part two: the Workaround

So now you know what may cause this behavior. Fortunately this shouldn't stop you from using a DataGrid, because the workaround is simple. All you need to do is embed a style in your DataGrid that resets the button margin to zero:

<toolkit:DataGrid>

<!-- Workaround for bug: No button margins allowed -->

<Style TargetType="{x:Type Button}">

<Setter Property="Margin" Value="0" />

</Style>

<!-- End of Workaround -->

<toolkit:DataGrid.Columns>

<toolkit:DataGridTextColumn Header="Test" />

</toolkit:DataGrid.Columns>

</toolkit:DataGrid>

This is just one of the many inconveniences with the current version of the toolkit. I strongly advise you to regularly check the issue tracker if you're planning to use its components in production.