C# 为什么我的System.Windows.Data.Binding因DependencyProperty而失败?

C# 为什么我的System.Windows.Data.Binding因DependencyProperty而失败?,c#,data-binding,dependency-properties,C#,Data Binding,Dependency Properties,我为我的对象获得了以下代码(短版本): 然后我创建另一个类,其中int作为dependencProperty。这是代码(也是简化版) 最后,我创建了一个PluginClass实例,我想将我的“MyInt”值绑定到MyClass的int。这是我得到的(也是简化版) 没有编译问题,但绑定无效。 总而言之,我不知道理论上是否必须得到: binding.Source = PluginClass.MyInt; binding.Path = new PropertyPath("???"); // don't

我为我的对象获得了以下代码(短版本):

然后我创建另一个类,其中int作为dependencProperty。这是代码(也是简化版)

最后,我创建了一个PluginClass实例,我想将我的“MyInt”值绑定到MyClass的int。这是我得到的(也是简化版)

没有编译问题,但绑定无效。 总而言之,我不知道理论上是否必须得到:

binding.Source = PluginClass.MyInt;
binding.Path = new PropertyPath("???"); // don't know what to "ask"

我认为第二种方法是好的,但我不知道为什么它不起作用:(
非常感谢您的帮助!

您的
PluginClass
应该实现。目前,绑定不知道
MyInt
的值已经更改


实现INPC将允许您的类在值更改时通知绑定(您必须在设置的
MyInt
函数中提高
PropertyChanged
)。

您的
inclugass
应该实现。目前,绑定不知道
MyInt
的值已更改


实现INPC将允许您的类在值更改时通知绑定(您必须在设置的
MyInt
函数中提高
PropertyChanged
)。

我曾考虑过它,但我认为它是另一个东西!将尝试一下:)我在考虑它,但我认为它是另一个东西!将尝试一下:)
public class MyClass : FrameworkElement
{
    public int Value
    {
        get
        {
            return GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(int), typeof(MyClass ), new PropertyMetadata(0));

    public MyClass(object source, string propertyName)
    {
        var b = new System.Windows.Data.Binding();
        b.Source = source;
        b.Path = new PropertyPath(propertyName);
        b.Mode = System.Windows.Data.BindingMode.TwoWay;

        SetBinding(ValueProperty, b);
    }
}
PluginClass pc = new PluginClass();
MyClass mc = new MyClass(pc, "MyInt");
binding.Source = PluginClass.MyInt;
binding.Path = new PropertyPath("???"); // don't know what to "ask"
binding.Source = PluginClass;
binding.Path = new PropertyPath("MyInt");