WPF DataGrid with DataGridTemplateColumn and RadioButton

Oct 12, 2009 at 10:00 AM
Edited Oct 12, 2009 at 11:49 AM

I have a DataGrid which represents a small number of options, one of which is chosen by default. In the first column of the DataGrid I want to have a RadioButton in each row. I have almost got this to work with the code below - the Binding is working. The problem is it is possible to get into a scenario where the setting from the RadioButton is lost.

Our underlying object being bound to implements IEditableObject and we use this in various way to check edits and also take action when the default is changed. I have found that clicking the RadioButton completely bypasses the BeginEdit / EndEdit process of the DataGrid.

The xaml is as follows:

                        <toolkit:DataGrid Name="dgTesters" AutoGenerateColumns="False" AlternationCount="2" ItemsSource="{Binding LocalTesters}" 
                                                  CanUserAddRows="false" CanUserDeleteRows="False"  
                                                  SelectionMode="Single" 
                                                  RowEditEnding="dgTesters_RowEditEnding">
                            <toolkit:DataGrid.Columns>
                                <toolkit:DataGridTemplateColumn>
                                    <toolkit:DataGridTemplateColumn.CellEditingTemplate>
                                        <DataTemplate >
                                            <RadioButton IsChecked="{Binding Path=IsDefault, Mode=TwoWay}" GroupName="a" />
                                        </DataTemplate>
                                    </toolkit:DataGridTemplateColumn.CellEditingTemplate>
                                    <toolkit:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate >
                                            <RadioButton IsChecked="{Binding Path=IsDefault, Mode=TwoWay}" GroupName="a" />
                                        </DataTemplate>
                                    </toolkit:DataGridTemplateColumn.CellTemplate>
                                </toolkit:DataGridTemplateColumn>
                                <toolkit:DataGridTextColumn Binding="{Binding TesterName}" Header="Tester Name" Width="*" MinWidth="80" />
                                <!-- Further columns deleted -->
</toolkit:DataGrid.Columns> </toolkit:DataGrid>
I think what is happening is that the CellTemplate is handling the RadioButton click, so the CellEditingTemplate never gets to fire BeginEdit... Does that make sense?
If I remove the DataGridTemplateColumn.CellTemplate, I can see the BeginEdit / EndEdit being fired.
This is also being used in a MVVM pattern, so I wanted to avoid handling mouseclicks.
It seems using a RadioButton in this way would be a fairly common requirement - I must be missing something. Can anyone point me in the right direction?
Thanks,
Tim

Oct 22, 2009 at 7:37 AM

Can anyone provide some pointers on this problem?

Thanks

Tim