Setters not run on Dependency Properties?

Jiew Meng picture Jiew Meng · Nov 19, 2010 · Viewed 16.1k times · Source

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);

Answer

Dean Chalk picture Dean Chalk · Nov 19, 2010

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).