Setter 不在依赖属性上运行?
只是一个简短的问题,澄清一些疑问。当元素绑定到依赖属性时,setter 是否不会运行?
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}" />
我注意到我的设置器中的以下内容没有运行
Debug.WriteLine("Setting value of TextContent: " + value);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 绑定引擎直接调用
GetValue
和SetValue
(绕过属性 setter 和 getter)。您需要该属性存在,以便 XAML 标记可以支持它(并正确编译)。The WPF binding engine calls
GetValue
andSetValue
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).要创建 DependencyProperty,请将 DepdencyProperty 类型的静态字段添加到您的类型中,并调用 DependencyProperty.Register() 以创建依赖项属性的实例。 DependendyProperty 的名称必须始终以 ...Property 结尾。这是 WPF 中的命名约定。
要使其可作为普通 .NET 属性进行访问,您需要添加属性包装器。该包装器除了使用从 DependencyObject 继承的 GetValue() 和 SetValue() 方法并传递 DependencyProperty 作为键在内部获取和设置值之外,什么也不做。
不要向这些属性添加任何逻辑,因为仅当您从代码设置属性时才会调用它们。如果从 XAML 设置属性,则直接调用 SetValue() 方法。
每个 DependencyProperty 都提供用于更改通知、值强制和验证的回调。这些回调在依赖属性上注册。
来源:http://www.wpftutorial.net/DependencyProperties.html
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