WPF-属性概述

时间:2021-02-23 14:15:45   收藏:0   阅读:0

翻译自微软官网

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/dependency-properties-overview?view=netframeworkdesktop-4.8

1、依赖属性和CLR属性

在WPF中,属性是标准的.NET属性。你和直接和这些属性交互,而不需要知道他们是依赖属性。但是,你应该熟悉依赖属性部分的或者全部的特征,这样你就可以利用这些特征。

The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree. In addition, a dependency property can be implemented to provide self-contained validation, default values, callbacks that monitor changes to other properties, and a system that can coerce property values based on potentially runtime information. Derived classes can also change some specific characteristics of an existing property by overriding dependency property metadata, rather than overriding the actual implementation of existing properties or creating new properties.

依赖属性基于其它输入的值来计算自己的值。这些其它输入包括系统属性例如主题、用户偏好,即时编译属性例如数据绑定、动画等,用户模板例如资源和样式,或者在元素树种的父子关系的属性值。另外,依赖属性还可以自我包含验证、默认值、监视属性值改变的回调函数。继承类可以通过覆盖属性元数据,改变一些依赖属性的特定的特征,而不是重新创建新的属性。

In the SDK reference, you can identify which property is a dependency property by the presence of the Dependency Property Information section on the managed reference page for that property. The Dependency Property Information section includes a link to the DependencyProperty identifier field for that dependency property, and also includes a list of the metadata options that are set for that property, per-class override information, and other details.

在SDK手册中,你可以通过管理引用页中的Dependecy Propertly Infomation指定那个属性为依赖属性。那个Dependecy Property Information部分包含了一个指向DependencyProperty标识的连接,这个连接指向一个字段来表示那个依赖属性,也包含依赖属性的元数据列表,每个类一个。

2、Dependency properties back CLR properties依赖属性支持CLR属性

Dependency properties and the WPF property system extend property functionality by providing a type that backs a property, as an alternative implementation to the standard pattern of backing the property with a private field. The name of this type is DependencyProperty. The other important type that defines the WPF property system is DependencyObjectDependencyObject defines the base class that can register and own a dependency property.

依赖属性和WPF属性系统通过一个类来扩展属性的功能,使用一个私有的字段来实现这些功能。这个类就是依赖属性,其它重要的定义在WPF属性系统中的是依赖类,依赖类定义了一个基类,这个基类可以注册和拥有一个依赖属性。

The following lists the terminology that is used with dependency properties:

下面列出来了依赖属性中使用的术语:

Dependency property: A property that is backed by a DependencyProperty.

依赖属性:通过DependecyProperty支持的属性

Dependency property identifier: A DependencyProperty instance, which is obtained as a return value when registering a dependency property, and then stored as a static member of a class. This identifier is used as a parameter for many of the APIs that interact with the WPF property system.

依赖属性标识符:一个依赖属性实例,通过注册依赖属性时的返回值获得,然后做为一个类的静态成员而存在。通其它的API交互时,这个标识符表示依赖属性。

CLR "wrapper": The actual get and set implementations for the property. These implementations incorporate the dependency property identifier by using it in the GetValue and SetValue calls, thus providing the backing for the property using the WPF property system.

CLR包装器:属性的get和set实现,这些实现组成了依赖属性标识符,因此在WPF属性系统中提供了属性的概念。

The following example defines the IsSpinning dependency property, and shows the relationship of the DependencyProperty identifier to the property that it backs.

下面的例子定义了IsSpinning依赖属性,并且展示了依赖属性标识符和它支持的属性的关系。

public static readonly DependencyProperty IsSpinningProperty =
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
    typeof(MyCode)
    );
public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

 The naming convention of the property and its backing DependencyProperty field is important. The name of the field is always the name of the property, with the suffix Property appended. For more information about this convention and the reasons for it, see Custom Dependency Properties.

属性的命名约定和它支持的依赖属性字段是重要的。字段名总是属性名带上Property后缀。更多关于约定理由请参考自定义依赖属性。

3、在XAML中设置依赖属性的值

<Button Background="Red" Content="Button!"/>
//或者这样
<Button Content="Button!">
  <Button.Background>
    <ImageBrush ImageSource="wavy.jpg"/>
  </Button.Background>
</Button>

4、在代码中设置依赖属性的值

Button myButton = new Button();
myButton.Width = 200.0;
double whatWidth;
whatWidth = myButton.Width;

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!