【问题标题】:DependencyProperty not Written in Derived Class [duplicate]未写在派生类中的DependencyProperty [重复]
【发布时间】:2013-04-04 15:44:28
【问题描述】:

我正在使用 WPF/XAML,并基于 window 定义了我自己的视图,称为 containercontainer 有一个名为 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


    【解决方案1】:

    我的问题是从不从 DerivedContainer 调用属性的 set 方法

    这不是问题 - 这是预期的行为。 WPF 直接调用SetValue 而不是通过包装器。包装器的存在是为了方便其他客户端代码。如果您需要在设置属性时执行某些操作,则需要在您的DependencyProperty.Register 调用中注册一个回调。

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2020-07-16
      • 2020-06-23
      • 2013-11-20
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多