一种风格依赖于另一种风格而激活?
我正在尝试创建一种样式,仅当该样式引用的元素的父元素具有其他特定样式时才应用该样式。有点像在 CSS 中,您可以执行“.class1 .class2”来指定“class2”主题仅在其位于类为“class1”的元素内时才适用。
我不希望使用任何形式的外部 DLL 或库来完成此任务。我想知道是否可以自己实现。
我尝试过使用 MultiTriggers 但没有成功。
我有一个适用于所有 TextBlock 的样式。我希望文本块执行以下操作:
如果文本块的字体大小为 11 并且父元素的样式为“PinnedSuggestion”,请将前景色设置为“#FF505050”。< /em>
如果文本块的字体大小为 11,且父元素的样式为“建议”,则将前景色设置为“#FFCCCCCC”。
我尝试编写以使这项工作正常进行,如下(字体大小条件为真,但另一个条件不是)。这些条件位于一般适用于所有文本块的样式内。
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FFFF5050"></Setter>
</MultiDataTrigger>
我不确定在这种情况下我做错了什么。下面您可以看到我的“建议”样式的 ListBoxItem 样式。 PinnedSuggestion 看起来完全一样(除了一些细微的变化)。
<Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Name="Container" Margin="0,0,0,0">
<Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
<Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3" SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
<ContentPresenter Margin="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
该 ListBoxItem 样式的 contentpresenter 包含我希望此技术使用的文本块。
所以,总结一下......
应用了“Suggestion”样式的 ListBoxItem 将在其中包含一个 TextBlock。 TextBlock 样式(由于其目标类型)将自动应用到该样式,并且我希望上述多重触发条件能够正常工作。
我的情况有点难以解释。我尽我所能地解释了一切。
I am trying to make a style that only gets applied if the parent element of the element that the style refers to, has another specific style. Kind of like in CSS where you can do ".class1 .class2" to specify that the "class2" theme only applies if it is within an element with the class "class1".
I do not wish to use any form of external DLLs or libraries for this task. I want to know if it's possible to implement on my own.
I've tried using MultiTriggers with no luck.
I have a style that applies to all TextBlocks. I want the textblock to do the following:
If the font-size of the textblock is 11 and the parent element's style is "PinnedSuggestion", set the foreground color to "#FF505050".
If the font-size of the textblock is 11 and the parent element's style is "Suggestion", set the foreground color to "#FFCCCCCC".
The conditions that I have tried to write to make this work, are as follows (the font-size condition is true, but the other one is not). The conditions are inside a style that applies to all textblocks in general.
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FFFF5050"></Setter>
</MultiDataTrigger>
I am not sure what I am doing wrong in this case. Below you see my ListBoxItem style for the "Suggestion" style. The PinnedSuggestion looks exactly the same (except for a few minor changes).
<Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Name="Container" Margin="0,0,0,0">
<Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
<Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3" SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
<ContentPresenter Margin="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The contentpresenter of that ListBoxItem style is what contains the textblocks that I want this technique to work with.
So, to summarize...
A ListBoxItem with the style "Suggestion" applied will have a TextBlock in it. The TextBlock style (due to its target type) will automatically apply to that, and I want the multitrigger conditions described above to work as they should.
My situation is kind of hard to explain. I explained everything as well as I could.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要检查其样式的父元素不是 TextBlock 的直接父元素;它可以在视觉树中任意更高。因此,您的第二个条件需要查找特定类型的祖先,如下所示:
这是一个完整的工作示例,使用 .NET4 进行了测试:
The parent element whose style you want to inspect is not the direct parent of the TextBlock; it could be arbitrarily higher in the visual tree. So your second condition needs to look for an ancestor of a particular type like this:
Here is a complete working example, tested with .NET4: