如何在依赖项属性中进行获取/设置没有任何作用? [英] Howcome the get/set in dependency property doesn't do anything?

查看:79
本文介绍了如何在依赖项属性中进行获取/设置没有任何作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个依赖项属性,例如:

I've created a dependency property like this:

public partial class MyControl: UserControl
{
   //...

   public static DependencyProperty XyzProperty = DependencyProperty.Register("Xyz",typeof (string),typeof (MyControl),new PropertyMetadata(default(string)));

   public string Xyz
   {
       get { return (string) GetValue(XyzProperty ); }
       set { SetValue(XyzProperty , value); }            
   }

   //...
}

然后将其绑定到我的wpf窗口,一切正常。

Then bind it to my wpf window and everything worked fine.

当我尝试向设置器中添加一些逻辑时,我注意到它没有被调用。我修改了get;现在设置如下:

When I tried to add some logic to the setter I notice it wasn't being called. I modify the get;Set up to a point now they look like this:

 get{return null;}
 set{}

它仍然有效!怎么会? GetValue / SetValue调用的用途是什么?

And it is still works! How come? What's the use of that GetValue/SetValue calls?

推荐答案

WPF数据绑定基础结构直接使用DependencyProperty,Xyz属性是程序员的便捷界面。

The WPF data binding infrastructure uses the DependencyProperty directly, the Xyz property is a convenience interface for the programmer.

看看 PropertyMetadata (在对DependencyProperty.Register的调用中),您可以提供一个在属性值更改时运行的回调,您可以在其中应用业务逻辑。

Take a look at the PropertyMetadata in the call to DependencyProperty.Register, you can supply a callback that will run when the property value is changed, this is where you can apply your business logic.

这篇关于如何在依赖项属性中进行获取/设置没有任何作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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