43

Just a short question, to clarify some doubts. Are setters not run when an element is bound to a dependency property?

public string TextContent
{
    get { return (string)GetValue(TextContentProperty); }
    set { SetValue(TextContentProperty, value); Debug.WriteLine("Setting value of TextContent: " + value); }
}

public static readonly DependencyProperty TextContentProperty =
    DependencyProperty.Register("TextContent", typeof(string), typeof(MarkdownEditor), new UIPropertyMetadata(""));

...

<TextBox Text="{Binding TextContent}" />

As I noticed the below in my setter does not run

Debug.WriteLine("Setting value of TextContent: " + value);
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • if you put a breakpoint in the setter you can see where it gets set. – VoodooChild Nov 19 '10 at 13:24
  • 6
    If you need to execute code when the property is set, look at the CoerceValueCallback or ValueChangedCallback of the UIMetadata class. – Jens Nov 19 '10 at 14:04
  • possible duplicate of [WPF: XAML property declarations not being set via Setters?](http://stackoverflow.com/questions/3836076/wpf-xaml-property-declarations-not-being-set-via-setters) – Tim Pohlmann Sep 14 '15 at 13:30
  • @Jens, your comment saved my day. I'm relatively new to WPF and dependency objects and properties. I had figured that the C# property wasn't doing much and was trying to fix this through `IValueConverter`, but had trouble setting a parameter (which can change too). In my case, one dependency property changes 2 more and the UI is expected to update correspondingly. `ValueChangedCallback` was the solution. I was looking for it in the `DependencyProperty.Register` function, didn't realise it was part of the metadata, but now I can see why. Thanks again. – nurchi Apr 13 '22 at 13:21

2 Answers2

59

The WPF binding engine calls GetValue and SetValue directly (bypassing the property setters and getters). You need the property to be there so it can be supported in the XAML markup (and compile correctly).

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • Hi, I'm not sure to understand what you say? – Emixam23 Sep 11 '16 at 17:01
  • @Emixam23 Dean is saying that the OP cannot put a debug statement in the TextContent property to determine whether or not the DP is being bound properly, because WPF will call SetValue, not the TextContent setter. – Dave Dec 06 '16 at 23:26
55

To create a DependencyProperty, add a static field of type DepdencyProperty to your type and call DependencyProperty.Register() to create an instance of a dependency property. The name of the DependendyProperty must always end with ...Property. This is a naming convention in WPF.

To make it accessable as a normal .NET property you need to add a property wrapper. This wrapper does nothing else than internally getting and setting the value by using the GetValue() and SetValue() Methods inherited from DependencyObject and passing the DependencyProperty as key.

Do not add any logic to these properties, because they are only called when you set the property from code. If you set the property from XAML the SetValue() method is called directly.

Each DependencyProperty provides callbacks for change notification, value coercion and validation. These callbacks are registered on the dependency property.

source: http://www.wpftutorial.net/DependencyProperties.html

Kishore Kumar
  • 21,449
  • 13
  • 81
  • 113