【发布时间】:2013-04-04 15:44:28
【问题描述】:
我正在使用 WPF/XAML,并基于 window 定义了我自己的视图,称为 container。 container 有一个名为 CurrentElementProperty 的 DependencyProperty,链接到属性 CurrentElement。我的问题是从未从DerivedContainer 调用该属性的set 方法。如果我在属性注册期间将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);
}
}
}
DerivedContainer 的 XAML(我删除了标准 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();
}
}
【问题讨论】:
标签: c# wpf xaml inheritance dependency-properties