是什么属性和依赖属性之间的区别 [英] What is the difference between Property and Dependency Property

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

问题描述

依赖特性创建的相同的方式的特性。

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.

的关系是不财产注册它注册在code中的其他具有约束力的属性后面。这是在我的项目使用的示例如下:

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

在上面的code中的ImageUri,是注册源的依赖项属性,即定义/内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绑定值应注册为code后面的依赖项属性。

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中已经定义的图像源应该是模板绑定来源,我们已经注册了相同的源代码
作为一个的DependencyProperty。

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.

我们不得不说这依赖属性的类型,在上面的例子中源为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.

现在设置depndency属性的值,通过如下我们的属性:

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);
 }

        }

现在这是怎么走,我们从后面我们code或XAML上面定义的源属性设置的值,并inturn它设置的关系是不财产ImageUri,这inturn设置在模板中值的值绑定源,因为我们已经注册ImageUri源,也就是presennt 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天全站免登陆