AttachedProperty 如何具有多个值?
AttachedProperty 是由所属父元素定义的单个属性,如何通过该父元素的多个子元素设置多个值?
例如:
如果我有:
<DockPanel>
<CheckBox DockPanel.Dock="Top">Hello</CheckBox>
<CheckBox DockPanel.Dock="Bottom">World</CheckBox>
</DockPanel>
这里我们有一个 DockPanel 元素,并且它有一个 Dock 属性。如何同时设置为“顶部”和“底部”?
How can an AttachedProperty which is a single property defined by an owning parent element, be set with multiple values through several child elements of that parent?
For example:
If I have:
<DockPanel>
<CheckBox DockPanel.Dock="Top">Hello</CheckBox>
<CheckBox DockPanel.Dock="Bottom">World</CheckBox>
</DockPanel>
Here we have a single DockPanel element and it has a single Dock property. How can it be set to "Top" and then "Bottom" simultaneously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它将最终出现在如下所示的方法中
正如您所看到的,它实际上不是在父级上设置的,而是通过 DockPanel 上的静态方法 SetDock 而不是父级实例在 CheckBox 本身上设置的。在代码后面执行此操作会使这一点更清晰,请注意我们如何从不使用 DockPanel 的实例。
希望这一点很清楚,除非您的问题是“在幕后”如何工作。在这种情况下,请参阅此问题。
引用自链接。
CheckBox 没有 Dock 属性的用途,除非它位于 DockPanel 中。 Grid.Row、Canvas.Left、Validation.HasError(只读)等也是如此。基本上,DockPanel 是需要信息的那个,但它需要它的所有子项都能够存储它。因此,它使用附加属性。如果您创建了一个名为 PuneetPanel 的新面板,并且需要一个 Angel 来计算子级位置,那么您可以在该面板内定义自己的附加属性 PuneetPanel.Angel,并且所有子级都可以使用它而无需子类化。
It will end up in a method looking like this
As you can see, it's actually not set on the parent, but the CheckBox itself, through the static method SetDock on DockPanel and not the parent instance. Doing it in code behind makes this a little clearer, notice how we never use an instance of a DockPanel.
Hopefully this was clear, unless your question was how this works "under the hood". In that case, see this question.
Quote from link.
A CheckBox has no use for a Dock property unless it is in a DockPanel. Same goes for Grid.Row, Canvas.Left, Validation.HasError (read only) etc. So basically, the DockPanel is the one needing the information, but it needs all its childs to be able to store it. Hence, it's using an Attached Property for it. If you created a new Panel, called PuneetPanel, and you needed an Angel to calculate the child position, then you could define your own Attached Property, PuneetPanel.Angel inside this panel and all childs could use this without having to be subclassed.
这是一个非常好的问题。答案在于 AttachedProperty 的工作原理。父级使用 AttachedProperty 来呈现子级。在渲染子级之前,父级会查找子级上定义的任何附加属性并将其应用于子级。
我从 msdn 中找到了这一点,这可能对您有用 ::
DockPanel 定义了 DockPanel.Dock 附加属性,并且 DockPanel 将类级代码作为其渲染逻辑的一部分(特别是 MeasureOverride 和 ArrangeOverride)。
DockPanel 实例将始终检查其任何直接子元素是否已为 DockPanel.Dock 设置了值。如果是这样,这些值将成为应用于该特定子元素的渲染逻辑的输入...
您可以查看此链接以获取详细概述 ::
http://msdn.microsoft.com/en -us/library/ms749011.aspx
希望对您有帮助!
This is a very nice question. The answer lies in how the AttchedProperty works. The AttachedProperty is used by the parent to render a child. Before rendering the child, the parent looks out for any attached property defined on child and applies to the child.
I found this from msdn which might be useful for you ::
DockPanel defines the DockPanel.Dock attached property, and DockPanel has class-level code as part of its rendering logic (specifically, MeasureOverride and ArrangeOverride).
A DockPanel instance will always check to see whether any of its immediate child elements have set a value for DockPanel.Dock. If so, those values become input for the rendering logic applied to that particular child element....
You can see this link to get detailed overview ::
http://http://msdn.microsoft.com/en-us/library/ms749011.aspx
Hope it helps you!!
对于您自己的自定义附加属性,有两个选项可以实现您正在寻找的内容:
1. 如果可设置值的组合数量不太复杂,您可以使您的附加属性具有枚举类型FlagsAttribute 设置。您可以使用按位或
|
组合要设置的值:及其在代码中的用法:
不过,这有一个小问题,您不能直接在 xaml 中执行上述操作,您必须编写 MarkupExtension 可以将字符串转换为标记的枚举值。它的用法将如下所示:
2. 由于附加属性可以是任何类型,它们当然也可以是复杂类型(具有多个子属性)甚至集合,因此很容易做到像这样:
如果您已经以这种方式定义了附加属性,则不需要任何 xaml 转换器,可以直接使用它:
For your own custom attached properties there are two options to achieve what you are looking for:
1. If the number of combinations of settable values are not to complex you could make your attached property of type enum that has the FlagsAttribute set. You can the combines the values you want to set using bitwise-or
|
:And its usage in code:
This has one small proplem though, you can not do the above in xaml directly, you would have to write a MarkupExtension that can convert string to flagged enum values. Its usage would then look like this:
2. Since attached properties can be of any type, they can of course also be complex types (with multiple subproperties) or even collections, so it is easily possible to do something like this:
If you have defined your attached property that way, you do not need any converters for xaml, you can use it directly: