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

时间秒杀一切 提交于 2020-01-01 07:27:10

问题


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:


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



回答2:


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




回答3:


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



回答4:


So here's what I came up with

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



回答5:


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.



来源:https://stackoverflow.com/questions/1631543/how-do-i-detect-row-selection-in-the-xceed-datagrid-for-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!