开发者

How do I detect row selection in the Xceed DataGrid for WPF

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

I'm using the Xceed 开发者_JS百科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.


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();
       }
}


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)...


You don't have to write complicated code for something simple... although it can become tedious, here is some code for you. I hope this helps:

<Style TargetType="xcdg:DataRow">
   <EventSetter Handler="dr_PreviewMouseDown" Event="PreviewMouseDown" />
</Style>

void dr_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
   DataRow dr = sender as DataRow;

   Debug.WriteLine(sender);
}


So here's what I came up with

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


I made for me a easiest way.

<xctk:MaterialButton Margin="5,0,5,0" Grid.Column="3" Content="Szűrt sorok kijelölése" Command="{Binding SelectFilteredRowsCommand}" CommandParameter="{Binding ElementName=MyDataGrid}" />

So, i send my datagrid with my commandparameter to the viewmodel.

 public RelayCommand<object> SelectFilteredRowsCommand { get; set; }

SelectFilteredRowsCommand = new RelayCommand<object>((o) =>
            {
                var datagrid = o as DataGridControl;
                if (datagrid != null)
                {
                    var datagriditems = datagrid.Items.Cast<SelectableProduct>();
                    foreach (SelectableProduct selectableProduct in datagriditems)
                    {
                        selectableProduct.IsSelect = true;
                    }
                }
            });

And convert back to datagrid itemsoruce type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜