1

I'm working with WPF/XAML and defined my own view based on window called container. container has a DependencyProperty called CurrentElementProperty linked to the property CurrentElement. My problem is that the set method of the property is never called from DerivedContainer. If I change the typeof(Container) to typeof(DerivedContainer) during property registering it gets called. I'm almost certain, I'm doing something wrong during registering.

Definition of 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 of DerivedContainer (I removed the standard XML namespace definitions)

<local:Container x:Class="ns.DerivedContainer"
                 xmlns:local="clr-namespace:ns">
  <local:Container.CurrentElement>
    <Element />
  </local:Container.CurrentElement>
</local:Container>

Code behind DerivedContainer

public partial class DerivedContainer : Container
{
    public DerivedContainer()
    {
        InitializeComponent();
    }
}
Daniel
  • 10,864
  • 22
  • 84
  • 115
denahiro
  • 1,211
  • 7
  • 10

1 Answers1

3

My problem is that the set method of the property is never called from DerivedContainer

This is not a problem - it is expected behavior. WPF calls SetValue directly rather than going through the wrapper. The wrapper is there for the convenience of other client code. If you need to do something when the property is set, you need to register a callback in your DependencyProperty.Register call.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393