要使用数据绑定设置属性的WPF自定义网格

要使用数据绑定设置属性的WPF自定义网格,wpf,dependency-properties,Wpf,Dependency Properties,我使用的是WPF,我有一个自定义的datagrid。我想做的是向该网格添加一个属性(CustomGridCommands),我可以从任何视图在xaml中设置该属性 我目前的情况如下(为了简化代码,我对代码做了一些修改): 自定义网格C#代码: 公共类CustomWPFDataGrid:DataGrid,INotifyPropertyChanged { 公共静态只读从属属性CustomContextMenuCommandsProperty= DependencyProperty.Register(

我使用的是WPF,我有一个自定义的datagrid。我想做的是向该网格添加一个属性(CustomGridCommands),我可以从任何视图在xaml中设置该属性

我目前的情况如下(为了简化代码,我对代码做了一些修改):

自定义网格C#代码:

公共类CustomWPFDataGrid:DataGrid,INotifyPropertyChanged
{
公共静态只读从属属性CustomContextMenuCommandsProperty=
DependencyProperty.Register(“CustomContextMenuCommand”,
类型(可观测采集),
类型(CustomWPFDataGrid));
[可装订(真实)]
公共可观察集合CustomContextMenuCommand
{
get{return(ObservableCollection)GetValue(CustomContextMenuCommandsProperty);}
set{SetValue(CustomContextMenuCommandsProperty,value);}
}
...
...
}
XAML代码:

<common:CustomWPFDataGrid 
        ItemsSource="{Binding Path=ItemList}"
        CustomContextMenuCommands="{Binding Path=CustomGridCommands, Mode=TwoWay}">
....
</common:CustomWPFDataGrid >

....
我绑定到包含栅格的视图的对象如下所示:

public class TestViewModel
{
    public ObservableCollection<TestDisplayViewModel> ItemList { get; set; }
    public ObservableCollection<WPFBaseCommand> CustomGridCommands;

    public TestViewModel()
    {
    ... population of objects here
    }
公共类TestViewModel
{
公共ObservableCollection项列表{get;set;}
公共observeCollection customgrid命令;
公共TestViewModel()
{
…这里的物体数量
}
当我运行此命令并检查datagrid中属性(CustomContextMenuCommand)的值时,它始终为null

你知道我做错了什么吗

编辑
“CustomContextMenuCommands”的设置程序永远不会被点击。

CustomGridCommands
在您的ViewModel中是一个字段,视图无法使用它。如果将其设置为公共属性,则它将变得可访问。有关可用作绑定源的详细信息,请访问


如果使用WPF 4.5,静态属性也可以用于绑定,如中所述。

好吧,ViewModel中的CustomGridCommand是私有的,View无法使用。啊,很抱歉,这是一个打字错误。我在问题中手工输入了它。它实际上是公共的。但是……它是一个公共变量,而不是公共属性。这就解决了它。看到你发给我,我在正确的方向上…你想把它贴出来作为答案,我会接受吗?
public class TestViewModel
{
    public ObservableCollection<TestDisplayViewModel> ItemList { get; set; }
    public ObservableCollection<WPFBaseCommand> CustomGridCommands;

    public TestViewModel()
    {
    ... population of objects here
    }