相同目标类型的多种样式(Precedence of multiple styles for the same target type?)

编程入门 行业动态 更新时间:2024-10-15 18:25:11
相同目标类型的多种样式(Precedence of multiple styles for the same target type?)

如果有这样的多个样式:

<Style TargetType="{x:Type local:MyControl}"> ...

是从资源字典中合并而来的,这些资源字典的目标应该是相同类型的控制,应该优先考虑,它是第一种遇到的还是最后一种?

我在哪里可以找到管理这些事情的规则?

If there are multiple styles like this:

<Style TargetType="{x:Type local:MyControl}"> ...

that are merged in from resource dictionaries that target the same type of control, which should have precedence, is it the first style encountered or last one?

Where can I find the rules that govern such things?

最满意答案

从最接近相关控件的ResourceDictionary应用样式。 一个例子:

<Window> <Window.Resource> <Style 1/> <Window.Resources> <Grid> <Grid.Resources> <Style 2/> </Grid.Resources> <TextBox/> </Grid> </Window>

在上面的例子中,样式2将应用于TextBox。 如果希望级联样式(将两种样式应用于TextBox),可以使用BasedOn="{StaticResource {x:Type TextBox}}" Style2上的BasedOn设置为指向Style1。 请检查语法,我没有在这里VS。

正如你所看到的,这个类型变成了Key 。 由于不允许在一个ResourceDictionary具有两个具有相同键的元素,因此无法合并两个具有重叠样式的ResourceDictionaries。 应该可以围绕这样的需求进行设计,记住ResourceDictionary可以引用其他用途。 再次,您使用BasedOn 。

Styles are applied from the ResourceDictionary closest to the control in question. An example:

<Window> <Window.Resource> <Style 1/> <Window.Resources> <Grid> <Grid.Resources> <Style 2/> </Grid.Resources> <TextBox/> </Grid> </Window>

In the above example, Style 2 will be applied to the TextBox. Should you wish to cascade the styles (apply both styles to the TextBox), you can set BasedOn on Style2 to point to Style1 using BasedOn="{StaticResource {x:Type TextBox}}". Please check the syntax, I don't have VS here.

As you can see, the type becomes the Key. Since it is not permissible to have two elements with the same key in a single ResourceDictionary, you cannot merge two ResourceDictionaries with overlapping styles. It should be possible to design around such a requirement, remembering that a ResourceDictionary can reference another use. Again, you use BasedOn.

更多推荐

应该,MyControl,字典,Type,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-07-15 17:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   多种   目标   类型   Precedence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!