财产和依赖财产有什么区别 [英] What is the difference between Property and Dependency Property

查看:15
本文介绍了财产和依赖财产有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

依赖属性的创建方式与属性相同.

Dependency properties are created the same way as properties.

是否仅在创建自定义控件时使用依赖属性?

Is a dependency property used only while creating a custom control?

推荐答案

依赖属性是一个注册另一个属性的属性(不是它本身,而是依赖于另一个,比如 XAML 绑定属性).

Dependency property is a property (not itself, but dependent on another, let’s say a XAML Binding property) which register another property.

dependecy 属性通过注册在后面的代码中注册另一个绑定属性.我的项目中使用的一个例子如下:

The dependecy property register the other binding property in the code behind by registering it. A example that is used in my project is as follows:

public static DependencyProperty ImageUri = DependencyProperty.Register("Source", typeof(BitmapImage), typeof(CustomImagePlaceHolder), new PropertyMetadata(null));

在上面的代码中,ImageUri 是一个注册 Source 的依赖属性,它在 generic.xaml 中定义/声明(不管是声明、定义还是其他任何东西),如下所示:

In the above code the ImageUri, is a dependency property which register the Source, that is defined/declared inside generic.xaml (whatever not sure whether declared, defined or anything else) as follows:

..HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
/>

所以这里非常重要的是,XAML 中的模板绑定值应该在后面的代码中注册为依赖属性.

So here it is quite important that the template binding value in the XAML should be registered as dependency property in the code behind.

所以当我们在 XAML 中定义 Image Source 应该与 Source 进行模板绑定时,我们注册了相同的 Source作为依赖属性.

So when we have defined in XAML that the Image Source should be template bind with Source, we have registered the same Source As a DependencyProperty.

我们不得不说依赖属性是哪种类型,在上面的例子中Source是BitmapImage的类型,所以我们定义了typeof(BitmapImage).

We have to say which type of dependency property is that, in above example the Source is the type of BitmapImage, so we have defined typeof(BitmapImage).

现在这个依赖属性的所有者/父对象是我们的 customControlClass CustomImagePlaceHolder,我们在注册时再次定义了它.

Now the owner/parent of this dependency property is our customControlClass CustomImagePlaceHolder, and we have defined that again while registering.

现在来设置依赖属性的值,使用我们的属性如下:

Now to set the value of depndency property, by using our properties as below:

public BitmapImage Source
        {
            get
            {

   string strURI = (string)GetValue(CustomImagePlaceHolder.ImageUri);
                return new BitmapImage(new Uri(strURI));
            }
            set
{
SetValue(CustomImagePlaceHolder.ImageUri, value);
 }

        }

现在是这样的,我们将后面的代码或 xaml 中的值设置为上面定义的 source 属性,然后它设置依赖属性 ImageUri 的值,它反过来设置模板绑定 Source 中的值,因为我们已将 ImageUri 注册为 Source,即现在的 generic.xaml.

Now this is how it go, we set the value from our code behind or xaml to the source property defined above, and inturn it sets the value of the dependecy property ImageUri, which inturn sets the value in the template binding Source, as we have registered ImageUri as Source, that is presennt generic.xaml.

这篇关于财产和依赖财产有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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