我正在使用 WPF/XAML 并基于window调用定义了我自己的视图container。container有一个 DependencyProperty 称为CurrentElementProperty链接到属性CurrentElement。我的问题是set该属性的方法永远不会从DerivedContainer. 如果我在属性注册期间更改为,它会被调用typeof(Container)。typeof(DerivedContainer)我几乎可以肯定,我在注册时做错了什么。
的定义Container
class Container : Window
{
public static readonly DependencyProperty CurrentElementProperty =
DependencyProperty.Register(
"CurrentElement",
typeof(Element),
typeof(Container),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.Inherits));
public Element CurrentElement
{
get
{
return (Element)this.GetValue(Container.CurrentElementProperty);
}
set
{
this.SetValue(Container.CurrentElementProperty, value);
}
}
}
XAML 的DerivedContainer(我删除了标准的 XML 命名空间定义)
<local:Container x:Class="ns.DerivedContainer"
xmlns:local="clr-namespace:ns">
<local:Container.CurrentElement>
<Element />
</local:Container.CurrentElement>
</local:Container>
背后的代码DerivedContainer
public partial class DerivedContainer : Container
{
public DerivedContainer()
{
InitializeComponent();
}
}