tags:

views:

354

answers:

3

I'm horrible at this WPF thing, so bear with me.

I'm using the Xceed DataGrid for WPF, and I need to know when someone selects a row, but I can't figure out how to do it. I'm sure I need to add some XAML to enable this, but I can't figure out what I should do.

+1  A: 

I'm actually struggling a bit with the same thing myself, except I have a prerequisite that the selection notification be done via an ICommand; however, if you do not have this need, you can wire up the SelectionChanged event handler. It's pretty elementary stuff, but I'll include the code just in case:

XAML:

 <Grid>
    <DataGrid:DataGridControl x:Name="gridControl" SelectionChanged="gridControl_SelectionChanged">
        <!-- Content -->
    </DataGrid:DataGridControl>
</Grid>

Code-behind:

private void gridControl_SelectionChanged(object sender, Xceed.Wpf.DataGrid.DataGridSelectionChangedEventArgs e)
        {
        var selectedIndex = gridControl.SelectedIndex; // int index
        var selectedItem = gridControl.SelectedItem;   // instance of bound object
        var selectedItems = gridControl.SelectedItems; // IList of bound objects
        }

All that said, I'm very interested to hear if there are any elegant solutions for getting the selected row from an Xceed DataGrid with an ICommand (in my case, I'm using anonymous types, which can make a difference)...

someweather
My Xceed grid doesn't have this event, so it wasn't really possible. Do you have the professional version?
Jonathan Beerhalter
Ah, yes, I do... didn't realize that they would limit the event handlers. Seems strange to me.
someweather
A: 

So here's what I came up with

    System.ComponentModel.DependencyPropertyDescriptor gridItemsSourceDescriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(DataGridControl.SelectedItemProperty, typeof(DataGridControl));
    gridItemsSourceDescriptor.AddValueChanged(dgBaxRuns, HandleSelectionChanged);
Jonathan Beerhalter
A: 

I use a MVVM approach and therefor favor data binding. I will bind the SelectedItem property to a SelectedItem property on my ViewModel object for the grid.

<xcdg:DataGridControl x:Name="grid" SelectedItem="{Binding SelectedItem}">
</xcdg:DataGridControl>

Then on your property setter can do what ever is necessary upon change in the SelectedItemChanged() method.

private IMyItem _selectedItem;
public IMyItem SelectedItem
{
   get { return _selectedItem; }
   set { 
          _selectedItem = value;
          OnPropertyChanged("SelectedItem");
          SelectedItemChanged();
       }
}
Mark Lindell