Friday, January 16, 2015

Radio buttons in DataGrids

You have probably noticed there is no <DataGridRadioButtonColumn> class in WPF. If you search online you will see people recommend using a <DataGridTemplateColumn> like this...

<DataGridTemplateColumn Header="Include">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <RadioButton HorizontalAlignment="Center" IsChecked="{Binding IsIncluded}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


Which is fine. But radio buttons normally come in groups. How do you create a group for each row? If we assume the DataGrid is bound to a collection that contains a uniquifier (a single column that uniquely identifies the row) then we can bind the GroupName property to that uniquifier like so...

GroupName="{Binding ID}"

No comments:

Post a Comment