Silverlight Datagrid Row Click

datagrid, silverlight

Solution

As is often the way i have found my own solution for this:

Add a MouseLeftButtonUp event to the datagrid:

<data:DataGrid x:Name="dgTaskLinks"
ItemsSource="{Binding TaskLinks}"
SelectedItem="{Binding SelectedTaskLink, Mode=TwoWay}"
MouseLeftButtonUp="dgTaskLinks_MouseLeftButtonUp"
>...

And walk the visual tree to get the data grid row:

private void dgTaskLinks_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                ///get the clicked row
                DataGridRow row = MyDependencyObjectHelper.FindParentOfType<DataGridRow>(e.OriginalSource as DependencyObject);

                ///get the data object of the row
                if (row != null && row.DataContext is TaskLink) 
                {
                    ///toggle the IsSelected value
                    (row.DataContext as TaskLink).IsSelected = !(row.DataContext as TaskLink).IsSelected;
                }

            }

Once found, it is a simple approach to toggle the bound IsSelected property :-)

Hope this helps someone else.

Problem

I have a datagrid with a column containing a checkbox. I want to change the value of the bound Selected property when the row is clicked: alt text http://lh4.ggpht.com/_L9TmtwXFtew/Sw6YtzRWGEI/AAAAAAAAGlQ/pntIr2GU6Mo/image_thumb%5B3%5D.png NOTE: I don't want to use the SelectedItemChanged event because this doesn't work properly when there is only one row in the grid.

Original source