使用单一样式设置 Hyperlink 和 TextBlock 的样式?
我有两种类型的文本,需要遵循基于枚举的类似着色规则:
public enum Modes
{
A,
B,
C
}
带有 DataTrigger
标记的样式用于着色:
<Style TargetType="SEE BELOW" x:Key="Coloring">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.}" Value="A">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="B">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="C">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
一种使用场景是 System.Windows.Documents.Hyperlink
带有嵌套的 System.Windows.Controls.TextBlock
:
<Hyperlink><TextBlock/></Hyperlink>
另一个只是一个简单的 TextBlock
:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
当然,我可以对两个 TextBlock 进行样式设置
元素:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink><TextBlock Style="{StaticResource Coloring}"/></Hyperlink>
但这无法设置超链接大小写下划线的样式。
如果我尝试跨两种类型设置样式:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink Style="{StaticResource Coloring}"><TextBlock/></Hyperlink>
则样式会失败,因为(显然)没有可在样式的 TargetType
属性中使用的共同祖先类型。
由于这最终应该是可配置的,因此目标是拥有一个 XAML 文档来定义这些文本块的颜色映射模式。因此,我不愿意有两种定义相同映射的冗余样式(一种用于超链接,一种用于文本块)。
所以...问题:如何在没有冗余 Style XAML 块的情况下对这两种情况保持一致的样式?
I have two types of text that need to follow similar coloring rules based on an enumeration:
public enum Modes
{
A,
B,
C
}
A Style with DataTrigger
markup is used to colorize:
<Style TargetType="SEE BELOW" x:Key="Coloring">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.}" Value="A">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="B">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=.}" Value="C">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
One use scenario is a System.Windows.Documents.Hyperlink
with a nested System.Windows.Controls.TextBlock
:
<Hyperlink><TextBlock/></Hyperlink>
and the other is just a simple TextBlock
:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
I can, of course, style both TextBlock
elements:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink><TextBlock Style="{StaticResource Coloring}"/></Hyperlink>
but that fails to style the underline of the Hyperlink case.
If I try to style across both types:
<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink Style="{StaticResource Coloring}"><TextBlock/></Hyperlink>
Then the styling fails, as there is (apparently) no common ancestor type for use in the TargetType
attribute of the Style.
Since this is suppposed to ultimately be a configurable thing, the goal is to have an XAML document that defines a mode to color mapping for these text blocks. Thus I am reluctant to have two redundant styles (one for Hyperlink and one for TextBlock) that define the same mapping.
So...the question: How can I consistently style both cases without redundant Style XAML blocks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以通过将超链接绑定到样式本身内来强制超链接与其父 TextBlock 具有相同的前景色,如下所示:
在本示例中,我添加了一个设置器来使默认前景为橙色,仅用于测试目的。
You can force Hyperlinks to have the same foreground colour as their parent TextBlocks by binding them within the style itself, like this:
In this example I've added a setter to make the default foreground Orange, just for testing purposes.