本文介绍了WPF Datagrid:SelectionUnit =“Cell”时不会引发SelectionChanged事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用WPF工具包数据网格。我将它设置为 SelectionUnit =Cell和 SelectionMode =Extended。SelectionChanged事件是没有提高!
当SelectionUnit设置为FullRow时,它工作正常。
我缺少某些东西? / p>
BTW,我需要的原因是因为我试图创建一个附属属性来帮助我将SelectedCells绑定到我的ViewModel。
解决方案
使用 DataGrid.SelectedCellsChanged
应该为您提供您需要的东西。
private void DG1_SelectedCellsChanged(object sender,SelectedCellsChangedEventArgs e)
{
//获取新选择的单元格
IList< DataGridCellInfo> selectedcells = e.AddedCells;
//获取每个新选择的单元格的值
foreach(选定单元格中的DataGridCellInfo di)
{
//将DataGridCellInfo.Item转换为源对象类型
//在这种情况下,ItemsSource是一个DataTable,单个项目是DataRows
DataRowView dvr =(DataRowView)di.Item;
//清除所有新选择的单元格的值
AdventureWorksLT2008DataSet.CustomerRow cr =(AdventureWorksLT2008DataSet.CustomerRow)dvr.Row;
cr.BeginEdit();
cr.SetField(di.Column.DisplayIndex,);
cr.EndEdit();
}
}
I'm using the WPF toolkit datagrid. I have it set to SelectionUnit="Cell" and SelectionMode="Extended".
The SelectionChanged event is never raised!
It works fine when the SelectionUnit is set to FullRow.
Am I Missing something?
BTW, the reason I need it is since I'm trying to create an Attached Property to help me bind the SelectedCells to my ViewModel.
解决方案
Make use of DataGrid.SelectedCellsChanged
which should provide you with what you need.
private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
//Get the newly selected cells
IList<DataGridCellInfo> selectedcells = e.AddedCells;
//Get the value of each newly selected cell
foreach (DataGridCellInfo di in selectedcells)
{
//Cast the DataGridCellInfo.Item to the source object type
//In this case the ItemsSource is a DataTable and individual items are DataRows
DataRowView dvr = (DataRowView)di.Item;
//Clear values for all newly selected cells
AdventureWorksLT2008DataSet.CustomerRow cr = (AdventureWorksLT2008DataSet.CustomerRow)dvr.Row;
cr.BeginEdit();
cr.SetField(di.Column.DisplayIndex, "");
cr.EndEdit();
}
}
这篇关于WPF Datagrid:SelectionUnit =“Cell”时不会引发SelectionChanged事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!