вторник, 5 октября 2010 г.

Silverlight get selected row data in DataGrid


The way I'd prefer to do this would be to bind IsChecked to a property of objects assigned to the ItemsSource. But here I'll show you the hard way to do it
(Edit: Actually the following is over complicated for this scenario but I'll leave it here for now, see edits after)
First you need one of my VisualTreeEnumeration extension methods:-
public static class VisualTreeEnumeration
{

    public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
    {
        DependencyObject current = VisualTreeHelper.GetParent(root);
        while (current != null)
        {
            yield return current;
            current = VisualTreeHelper.GetParent(current);
        }
    }
}
Now in my testing I've just added a ListBox with the name lstOutput to my Xaml. Now add the following couple of event handlers to you UserControl :-
    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", row.DataContext));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", row.DataContext));
    }
and finally tweak the Radio button Xaml like so:-
<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup"
    Checked="rdbIndataGrid_Checked" Unchecked="rdbIndataGrid_Unchecked"  />
(One of the neat things about Xaml wiring up events is that it works even when the elements are part of a Template).
You'll note that in the event handlers I'm walking up the visual tree from the sending RadioButton to find the containing DataGridRow. The DataGridRow is the object that its DataContext set to the object being rendered by that row. In your own code you could cast the data context value to the correct type and from there access other data about the row.
Edit
Actually in most ordinary cases you don't need to hunt down the owning DataGridRow object accessing the DataContext property of the sending RadioButton is sufficient:-
    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;

        if (myData != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", myData));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;
        if (myData != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", myData));
    }
Hence you can dispense with the Ancestors extension method. However in more complex cases where the DataContext have been changed the original "over-complicated" approach may be needed.

Комментариев нет: