tags:

views:

38

answers:

1

I may be doing this all wrong... so hang with me

I am making a user control with a property which the user can bind to. The in setter for the property, I bind the PropertyChanged listener to the property so I can react to changes to its state. The code behind for this user control looks like this:

        public static readonly DependencyProperty NodeProperty =
        DependencyProperty.Register("Node", typeof(MockRequirementWrapper), typeof(RecNode2));
    public MockRequirementWrapper Node
    {
        get
        {
            return (MockRequirementWrapper)GetValue(NodeProperty);
        }
        set
        {
            if(Node != null)
                Node.PropertyChanged -= Update;
            SetValue(NodeProperty, value);
            Node.PropertyChanged += new PropertyChangedEventHandler(Update);
            OnPropertyChanged(this, "Node");
        }
    }

then, in another user control, I bind to this property a node I've created elsewhere like this:

<local:RecNode2 Node="{Binding}"/>

What I am finding is the recnode exists and is bound to a node... but if I put a breakpoint in the setter, it never gets called. Am I misunderstanding how the binding works? How do I add my listener when the node changes?

+1  A: 

The framework will always call GetValue and SetValue directly, the property is just for convenience and sould never contain logic besides those calls. If you want to do something on changes register a PropertyChangedCallback in the Metadata when registering the DependencyProperty.

Taken from http://msdn.microsoft.com/en-us/library/ms753358.aspx:

public static readonly DependencyProperty AquariumGraphicProperty = DependencyProperty.Register(
  "AquariumGraphic",
  typeof(Uri),
  typeof(AquariumObject),
  new FrameworkPropertyMetadata(null,
      FrameworkPropertyMetadataOptions.AffectsRender, 
      new PropertyChangedCallback(OnUriChanged)
  )
);
public Uri AquariumGraphic
{
  get { return (Uri)GetValue(AquariumGraphicProperty); }
  set { SetValue(AquariumGraphicProperty, value); }
}
MrDosu