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
,但我不喜欢它