Monday, March 15, 2010

How to select a row in the WPF DataGrid using a CheckBox

This is fairly simple to achieve in the WPF DataGrid, all you need to do is add a DataGridCheckBoxColumn to the Columns collection of the data grid, and then bind it to the IsSelected property of the DataGridRow that is hosting it.
<Grid.Resources>
    <local:PersonCollection x:Key="people">
        <local:Person ID="1" FirstName="Joe" LastName="Blow" />
        <local:Person ID="2" FirstName="Sam" LastName="Blam" />
        <local:Person ID="3" FirstName="Jack" LastName="Black" />
        <local:Person ID="4" FirstName="Hugo" LastName="Blue" />
    </local:PersonCollection>
</Grid.Resources>

<DataGrid
    x:Name="dataGrid"
    ItemsSource="{Binding Source={StaticResource people}}"
    IsSynchronizedWithCurrentItem="True"
    >
    <DataGrid.Columns>
        <DataGridCheckBoxColumn 
            Binding="{Binding IsSelected, 
                RelativeSource={RelativeSource AncestorType=DataGridRow}}" 
            />
    </DataGrid.Columns>
</DataGrid>  

1 comment:

  1. This does work however it doesnt select multiple rows

    ReplyDelete

Note: Only a member of this blog may post a comment.