C# 如何以编程方式绑定DataGrid.SelectedCells

C# 如何以编程方式绑定DataGrid.SelectedCells,c#,wpf,datagrid,datagridcell,C#,Wpf,Datagrid,Datagridcell,有人知道怎么做吗 我知道如何用代码绑定 DataGrid DGrid = new DataGrid(); DGrid.SetBinding( ... , myBinding); 但不允许使用SelectedCells,因为存在非DataGrid。SelectedCellsProperty我只找到了DataGrid。SelectedIndexProperty和DataGrid。SelectedItemProperty 任何能帮上忙的事都将不胜感激 如果您有其他依赖属性,可以将其绑定到DataGr

有人知道怎么做吗

我知道如何用代码绑定

DataGrid DGrid = new DataGrid();
DGrid.SetBinding( ... , myBinding);
但不允许使用
SelectedCells
,因为存在非
DataGrid。SelectedCellsProperty
我只找到了
DataGrid。SelectedIndexProperty
DataGrid。SelectedItemProperty


任何能帮上忙的事都将不胜感激

如果您有其他依赖属性,可以将其绑定到
DataGrid.SelectedCells
属性。 例如,假设您有一些
DependencyObject
祖先,在它里面有一些
SelectedCellsInGrid
依赖属性,在这种情况下,您可以调用如下内容:

var binding=new binding(“SelectedCells”){Source=yourDataGrid};
此.SetBinding(.SelectedCellsInGridProperty,binding);

若您并没有依赖对象,那个么您可以在自定义编写的行为中实现这一点。如果这是你的情况,请留下评论-如果需要,我会提供一些额外的细节。

哦,我不想写一个行为,因为我觉得它看起来很混乱,所以如果我正确理解你,就必须创建一个
DependencyObject
,这也不是我所期望的。没有其他方法可以做到这一点吗?如果您试图从Window或UserControl中访问DataGrid控件,那么您可以在其中声明DependencyProperty(因为所有这些都派生自DependencyObject)。我在我的
视图模型中构建我的
数据网格
,为我的
视图
提供它作为
属性
,现在我将使用同一
视图模型中的另一个
属性
绑定到
所选单元格
,这就是我想要的:)。当然,我可以“绑定”到
selectedcellshangedevent
,但我不喜欢它