WPF中的依赖属性使用 [英] Dependency Property Uses in WPF

查看:321
本文介绍了WPF中的依赖属性使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找出依赖项属性的充分理由。为什么System.Controls.TextBox的文本属性是依赖项属性而不是常规属性?作为依赖项属性有什么好处?

I am having a hard time figuring out good reasons for the dependency property. Why the System.Controls.TextBox "Text" property a dependency property and not a normal property? What benefits does it serve being a dependency property?

我要完成的一件事是在UserControl中添加一个ValidationRules属性,该属性将包含其他验证规则。像这里:

One of the things I am trying to accomplish is to add a ValidationRules property to my UserControl which will contain other validation rules. Like here:

<customControls:RequiredTextBox.ValidationRules>
                        <validators:NotNullOrEmptyValidationRule ErrorMessage="FirstName cannot be null or empty"/>
                    </customControls:RequiredTextBox.ValidationRules>

问题是我不确定ValidationRules属性是DependencyProperty还是普通属性。

The problem is that I am not sure if ValidationRules property should be DependencyProperty or just a normal property.

上面的代码给出以下错误:

The above code gives the following error:

{"Cannot add element to 'ValidationRules'; the property value is null.  Error at object 'LearningWPF.ValidationRules.NotNullOrEmptyValidationRule' in markup file 'LearningWPF;component/addcustomerwindow.xaml' Line 35 Position 66."}

这是ValidationRules属性:

Here is the ValidationRules property:

 public static readonly DependencyProperty ValidationRulesProperty =
            DependencyProperty.Register("ValidationRules",
                                        typeof (Collection<ValidationRule>), typeof (RequiredTextBox),
                                        new FrameworkPropertyMetadata(null)); 

        public Collection<ValidationRule> ValidationRules
        {
            get { return (Collection<ValidationRule>)GetValue(ValidationRulesProperty); }
            set { SetValue(ValidationRulesProperty, value); }
        }


推荐答案

主要有两个好处折叠:

首先,只有在使用依赖项属性时,它才会创建,这意味着TextBox类可以非常有效,并且由于其数量很少,因此占用内存少不动产占用堆上的空间。这在WPF中尤其重要,因为WPF中的所有控件只是越来越多的特定类型的集合。如果每个内部类型都声明了数十个属性来定义行为并看上去,那么像按钮这样的高级控件最终将具有一个类的大小,并且具有数百个属性。

Firstly a dependency property is only created when it is used, this means the TextBox class can be very efficient, with a low memory footprint since it has a minimal number of real properties taking up space on the heap. This is especially important in WPF where all controls are just collections of more and more specific types. If each of those inner types declared tens of properties to define behaviour and look then a high level control like a button would end up having the size of a class with something in the ballpark of a hundred properties.

第二个依赖项属性可以绑定到对象,而不是为其创建类型的对象。这允许控件可以设置Grid.Column属性的情况,该属性可以被Grid控件读取并用于布局。这意味着我们没有数百个装饰器类提供其他控件所需的微小功能。这意味着xmal更加直观和可读。

Secondly dependency properties can be tied to an object other than the type that they are created for. This allows the case where a control can set a Grid.Column property, which the Grid control can read and use for layout. This means that we don't hundreds of decorator classes supplying tiny pieces of functionality required by other controls. This means that xmal is far more intuitive and readable.

经过修改,可解决您修改后的问题中的示例:

Edited to address the example in your revised question:

虽然您的验证属性不会从依赖属性中获得太多好处(基本上是到目前为止所有答案的原因,但我只能真正看到我对内存占用的评论)在播放中),这当然不是有利的,因为在文本框的Text属性中,您可能希望绑定它,或者根据其他输入对其进行更改,我仍然会将其实现为依赖项属性。我的理由很简单;您不会获得太多收益,但也不会花费您任何钱-我从来不希望自己在自定义控件中使用基本属性,而当我第一次开始编写它们时,我一直在不断将基本属性升级为依赖项,因为我想一些额外的功能。

While your validation property won't gain much benefit from being a dependency property (basically out of the reasons in all the answers so far I can only really see my comment of memory footprint coming in to play), and it certainly isn't advantageous as it is in the case of the Text property of a text box where you may want to bind it, or change it based on some other input, I would still implement it as a dependency property. My reasoning for this is simple; you don't gain much, but it also doesn't cost you anything - I have never wished I had used a basic property in a custom control whereas when I first started writing them I was constantly upgrading my basic properties to dependencies because I wanted some extra functionality.

简而言之,虽然dependency属性要定义一个普通属性更为复杂,但除非存在,否则我仍将其用作WPF控件的事实标准。是这样做的一些很好的理由。尽管字段更易于实现,但与属性几乎是类的标准一样。

Simply put, while the dependency property is more complex to define that a normal property I would still use it as the de facto standard for WPF controls unless there was some good reason to do otherwise. In much the same way as a property is the standard for classes, even though a field is easier to implement.

这篇关于WPF中的依赖属性使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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