While using a WPF DataGrid and a CheckBox column I realised that it was taking two Mouse clicks to change the state of the CheckBox.

I found a good post by Mike Borozdin which explains how to work around this ‘issue’;

http://www.mikeborozdin.com/post/WPF-DataGrid-CheckBox-Single-Click-CheckingUnchecking.aspx

Basically, if instead of adding a CheckBox column, you add a TemplateColumn, and enclose a CheckBox within the DataTemplate, this works around the ‘issue’;

<DataGrid Name="dgProducts" AutoGenerateColumns="False" CurrentCellChanged="dgProducts_CurrentCellChanged">

    <DataGrid.Columns>

        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>

        <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}"/>

        <DataGridTemplateColumn Header="In Stock">

            <DataGridTemplateColumn.CellTemplate>

                <DataTemplate>

                    <CheckBox IsChecked="{Binding Path=IsInStock, UpdateSourceTrigger=PropertyChanged}">

                </DataTemplate>

            </DataGridTemplateColumn.CellTemplate>

        </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>

By |2012-10-22T16:50:00+01:00October 22nd, 2012|.Net Framework, Bugs, WPF|0 Comments

About the Author:

Leave A Comment