如何将属性更改的回调添加到对象的Name属性?

时间:2021-06-14 16:44:56

Since I didn't register the property how would I add a property changed callback?

由于我没有注册属性,我将如何添加属性更改回调?

This works:

public static readonly DependencyProperty NameProperty =
    FrameworkElement.NameProperty.AddOwner(typeof(Node), new FrameworkPropertyMetadata("Node", new PropertyChangedCallback(NamePropertyChanged)));`

but there is a warning which I don't understand, so maybe there is another way of doing this:

但是有一个我不明白的警告,所以也许有另一种方法可以做到这一点:

WpfApplication1.Node.NameProperty' hides inherited member 'System.Windows.FrameworkElement.NameProperty'. Use the new keyword if hiding was intended.

WpfApplication1.Node.NameProperty'隐藏继承的成员'System.Windows.FrameworkElement.NameProperty'。如果要隐藏,请使用new关键字。

1 个解决方案

#1


0  

Your class Node seems to be derived from FrameworkElement (or a subclass of FrameworkElement). Both Node and FrameworkElement define

您的类Node似乎派生自FrameworkElement(或FrameworkElement的子类)。 Node和FrameworkElement都定义了

public static readonly DependencyProperty NameProperty;

which generates the compiler warning. Just write

它会生成编译器警告。写吧

public static new readonly DependencyProperty NameProperty ...

in class Node.

在类Node中。

You could also write

你也可以写

FrameworkElement.NameProperty.OverrideMetadata(
    typeof(Node),
    new FrameworkPropertyMetadata("Node", new PropertyChangedCallback(NamePropertyChanged)));

in Node's static constructor.

在Node的静态构造函数中。

#1


0  

Your class Node seems to be derived from FrameworkElement (or a subclass of FrameworkElement). Both Node and FrameworkElement define

您的类Node似乎派生自FrameworkElement(或FrameworkElement的子类)。 Node和FrameworkElement都定义了

public static readonly DependencyProperty NameProperty;

which generates the compiler warning. Just write

它会生成编译器警告。写吧

public static new readonly DependencyProperty NameProperty ...

in class Node.

在类Node中。

You could also write

你也可以写

FrameworkElement.NameProperty.OverrideMetadata(
    typeof(Node),
    new FrameworkPropertyMetadata("Node", new PropertyChangedCallback(NamePropertyChanged)));

in Node's static constructor.

在Node的静态构造函数中。