Setter 不在依赖属性上运行? [英] Setters not run on Dependency Properties?

查看:19
本文介绍了Setter 不在依赖属性上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简短的问题,澄清一些疑问.当元素绑定到依赖属性时,setter 是否不运行?

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}" />

因为我注意到我的 setter 中的以下内容没有运行

As I noticed the below in my setter does not run

Debug.WriteLine("Setting value of TextContent: " + value);

推荐答案

WPF 绑定引擎直接调用 GetValueSetValue(绕过属性 setter 和 getter).您需要该属性存在,以便在 XAML 标记中支持它(并正确编译).

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

这篇关于Setter 不在依赖属性上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆