wpf中DependencyProperty的解释

guoweijun394 2007-04-20 04:06:35
如题,看了windows sdk中的描述,还是不太了解这个类,希望知道的帮解释一下,谢谢~!
...全文
364 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangdong1900 2007-04-21
  • 打赏
  • 举报
回复
DependencyProperty---关于这个东西,网上一大堆说明。。
总能找到你想了解的地东西
huangdong1900 2007-04-21
  • 打赏
  • 举报
回复
我顶!!!
guoweijun394 2007-04-21
  • 打赏
  • 举报
回复
谁顶一下啊我结贴了
guoweijun394 2007-04-20
  • 打赏
  • 举报
回复
谁顶下 给分
guoweijun394 2007-04-20
  • 打赏
  • 举报
回复
这是SDK 中的说明

XAML Attribute Usage
<object property="dependencyPropertyName"/>
- or -
<object property="ownerType.dependencyPropertyName"/>
- or -
<object property="attachedPropertyOwnerType.attachedPropertyName"/>



XAML Values
dependencyPropertyName
A string that specifies the DependencyProperty.Name of the desired dependency property. This can be preceded by a prefix if the property is not in the default xmlns namespace (for details, see XAML Namespaces and Namespace Mapping.)

ownerType.dependencyPropertyName
A string that specifies an owner type of a dependency property, a dot (.), then the DependencyProperty.Name. ownerType can also be preceded by a prefix. This usage is particular to late-bound styles and templates, where the owner of the dependency property must be specified for parsing context because the TargetType is not yet known. For more information, see Styling and Templating.

attachedPropertyOwnerType.attachedPropertyName
A string that specifies the owner of an attached property, a dot (.), then the attached property name. attachedPropertyOwnerType can also be preceded by a prefix.

Remarks
If you want properties on your custom types to support value expressions, property invalidation, per-type default values, inheritance, data binding, animation, or styling, you should back these CLR properties with a dependency property following these guidelines and procedures:

Register a dependency property using the Register method; this method returns a DependencyProperty, which you should store as an accessible static read-only field in your class. By convention, the name of this DependencyProperty identifier field should end with Property, and this convention is followed by all dependency properties implemented in WPF. For example, the Control.Background dependency property is identified by the Control.BackgroundProperty field.

During registration, you can provide PropertyMetadata for the property to further define the property's behaviors, including its default value and callbacks that can be used to modify the property value in accordance to changes in related property values.

Provide common language runtime (CLR) accessors for the property.

If you want to create a property that can be used on all DependencyObject types regardless of whether they are within the derived class list of the class that defines the property, then you should create an attached property. An example of an attached property is the DockPanel.Dock property. For details, see Attached Properties Overview.

Dependency properties in general are described in more detail in the Dependency Properties Overview. Creating custom dependency properties is described in more detail in Custom Dependency Properties.

You can also create a DependencyProperty and use the property system to get and set the value without exposing common language runtime (CLR) accessors, but this is not a typical class design practice.

DependencyProperty supports a XAML attribute syntax for filling property values, which is used when a Setter, Trigger, or Condition in a Style specifies its Property value. For details, see Styling and Templating.

Attached properties are available from any DependencyObject, and use a distinct ownerType.propertyName syntax form in XAML. For details, see Attached Properties Overview..

Example
This example shows how to back a common language runtime (CLR) property with a DependencyProperty field, thus defining a dependency property. When you define your own properties and want them to support many aspects of Windows Presentation Foundation (WPF) functionality, including styles, data binding, inheritance, animation, and default values, you should implement them as a dependency property.

The following example first registers a dependency property by calling the Register method. The name of the identifier field that you use to store the name and characteristics of the dependency property must be the Name you chose for the dependency property as part of the Register call, appended by the literal string Property. For instance, if you register a dependency property with a Name of Location, then the identifier field that you define for the dependency property must be named LocationProperty.

In this example, the name of the dependency property and its CLR accessor is State; the identifier field is StateProperty; the type of the property is Boolean; and the type that registers the dependency property is MyStateControl.

If you fail to follow this naming pattern, designers might not report your property correctly, and certain aspects of property system style application might not behave as expected.

You can also specify default metadata for a dependency property. This example registers the default value of the State dependency property to be false.

C# Copy Code
public class MyStateControl : ButtonBase
{
public MyStateControl() : base() { }
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set { this.SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
"State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
}


在Windows系统,notepad.exe(记事本)是一个“经典的”、“简洁的”文本编辑器。这个软件,没有华丽的外观,也没有繁杂的功能,仅仅是一个文本编辑小软件。虽然经过Windows系统数十年的变换,但它却保持着永恒姿态,数十年来几乎不曾改变过。曾经,VS的经典DEMO,就有它的身影,一个新建的项目,就藏有一个新建的“记事本”。然而,在WPF的项目,“记事本”却消失的无影无踪,也许是很容易实现,也许是为了革新,而不愿再传承“经典”。确实,使用WPF技术再次让“记事本”复活,确实也是一件非常容易的事情。但是,如果,使用WPF技术,再搭配当下非常流行的MVVM模式呢?复活“记事本”的难度却陡然上升至很多WPF程序员为之默默叹气。而,MVVM模式是掌握WPF的最顶级技术,MVVM模式拥有的无尽的优势,让WPF相对于过往的编程模式来说,是一种革命性的创新,从而也成为大WPF项目必须的模式。但,学习难度。。。。。。在这个《WPF记事本开发详解》的课程,赵老师带领你在WPF,从零开始一步步构建MVVM模式,直到让你亲自以WPF+MVVM的方式,让这个经典的“记事本”软件从你的手“复活”。在课程,赵老师会详细讲解WPF和MVVM的各种技巧,让你从此爱上WPF+MVVM编程。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧