Setter 不在依赖属性上运行?

发布于 2024-10-03 12:46:02 字数 653 浏览 9 评论 0原文

只是一个简短的问题,澄清一些疑问。当元素绑定到依赖属性时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-10-10 12:46:02

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

能怎样 2024-10-10 12:46:02

要创建 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文