附加属性在 WPF 中究竟是如何工作的? [英] How exactly do Attached Properties work in WPF?

查看:28
本文介绍了附加属性在 WPF 中究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对附加属性如何实际将其值传达给父元素或子元素感到有些困惑.TextElement.FontFamily 使子元素继承分配给该属性的值(看似下游操作,父对子).Grid.Column 使父项在特定位置显示该子项(看似上游的操作,子项到父项).附加属性值如何知道是向上流动还是向下流动?我对此的看法是错误的,还是缺少一个可以让所有这些都变得正确的部分?

I'm a bit mystified as to how Attached Properties actually convey their values to either parent or child elements. TextElement.FontFamily causes child elements to inherit the value assigned to that property (a seemingly downstream operation, parent to child). Grid.Column causes a parent item to display that child in a particular position (a seemingly upstream operation, child to parent). How do Attached Property values know to either flow up or down? Is my conception of this incorrect, or is there a piece missing that will put all of this into perspective?

<StackPanel TextElement.FontFamily="Wingdings">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="1" Content="My Button"/>
    </Grid>
</StackPanel>

推荐答案

这里有两个概念:依赖属性附加依赖属性.附加属性"是依赖属性,因此支持依赖 property价值继承.

There are two concepts here: dependency properties and attached dependency properties. "Attached Properties" are dependency properties, and as such support dependency property value inheritance.

关于基本的依赖属性,一个非常粗略的说法是它们基本上从 wpf(逻辑/视觉)树中的父元素继承它们的值.依赖属性(附加或不附加)继承其值向下"如果它的 metadata 设置为 FrameworkPropertyMetadataOptions.继承标志,和在很多情况下就是这样.

About basic dependency properties, a very rough statement would be that they basically inherit their values from parent elements in the wpf (logical/visual) tree. A dependency property (attached or not) inherits its value "downwards" if its metadata is set with the FrameworkPropertyMetadataOptions.Inherit flag, and in many cases this is so.

附加属性是可以通过 DependencyObject.SetValue 方法.这种机制的目的是附加"父对象需要的其他对象信息,而不是子对象本身.例如,Grid.Row 是 Grid 在其渲染区域内放置项目所需的附加属性.

Attached properties are properties which can be set on any wpf object (basically, at least a DependencyObject) via the DependencyObject.SetValue method. The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves. For example, the Grid.Row is an attached property required by the Grid to place items within its render area.

依赖属性是向下"继承的由 wpf 对象系统自动执行.

Dependency properties are inherited "downwards" automatically by the wpf object system.

向上"检查附加属性明确地,在特定对象的代码中.对于 Grid,在确定放置项目的位置后,它会检查每个包含项目的 Grid.Row 和 Grid.Column 附加属性的值.

Attached properties are examined "upwards" explicitly, in the code of specific objects. In the case of Grid, upon determining where to place its items, it checks for the value of Grid.Row and Grid.Column attached properties on each contained item.

创建自定义附加属性通常也是一种技术,这些属性以某种方式修改它们附加到的对象(例如,通过附加属性的拖放功能).

It is also often the technique to create custom attached properties which modify in some way the objects they are attached to (for example, the Drag'n'Drop functionality via attached properties).

作为附加说明,继承附加属性的一个很好的例子是 TextElement.FontFamily.Grid.Row 和 Grid.Column 属性没有设置 Inherits 标志.

As an additional note, a good example of an inheriting attached property is TextElement.FontFamily. Grid.Row and Grid.Column properties do not have the Inherits flag set.

TextElement.FontFamily,来自反射器:

TextElement.FontFamily, from Reflector:

FontFamilyProperty = DependencyProperty.RegisterAttached("FontFamily", typeof(FontFamily), typeof(TextElement), new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(TextElement.IsValidFontFamily));

Grid.Row,来自反射器:

Grid.Row, from Reflector:

RowProperty = DependencyProperty.RegisterAttached("Row", typeof(int), typeof(Grid), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(Grid.OnCellAttachedPropertyChanged)), new ValidateValueCallback(Grid.IsIntValueNotNegative));

这篇关于附加属性在 WPF 中究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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