使用单一样式设置 Hyperlink 和 TextBlock 的样式?

发布于 2024-08-21 03:44:30 字数 1884 浏览 8 评论 0原文

我有两种类型的文本,需要遵循基于枚举的类似着色规则:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-08-28 03:44:30

您可以通过将超链接绑定到样式本身内来强制超链接与其父 TextBlock 具有相同的前景色,如下所示:

<Style TargetType="TextBlock" x:Key="Coloring">
        <Style.Resources>
            <Style TargetType="Hyperlink">
                <Setter Property="Foreground" Value="{Binding Foreground,RelativeSource={RelativeSource FindAncestor,AncestorType=TextBlock}}"/>
            </Style>
        </Style.Resources>
            <Setter Property="Foreground" Value="Orange"/>
        <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>

在本示例中,我添加了一个设置器来使默认前景为橙色,仅用于测试目的。

You can force Hyperlinks to have the same foreground colour as their parent TextBlocks by binding them within the style itself, like this:

<Style TargetType="TextBlock" x:Key="Coloring">
        <Style.Resources>
            <Style TargetType="Hyperlink">
                <Setter Property="Foreground" Value="{Binding Foreground,RelativeSource={RelativeSource FindAncestor,AncestorType=TextBlock}}"/>
            </Style>
        </Style.Resources>
            <Setter Property="Foreground" Value="Orange"/>
        <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>

In this example I've added a setter to make the default foreground Orange, just for testing purposes.

憧憬巴黎街头的黎明 2024-08-28 03:44:30

发帖后,我意识到了另一种方法。我强制使用嵌套 TextBlock 场景的超链接。如果我要将超链接包装在 TextBlock 中:

<TextBlock Style="{StaticResource Coloring}"><Hyperlink><TextBlock/></HyperLink></TextBlock>

那么我的两个案例都会折叠为 TextBlock 样式。 (结合上面的解决方案)

After posting, I realized another approach. I was forcing the Hyperlink with nested TextBlock scenario. If I were to wrap the the Hyperlink in a TextBlock:

<TextBlock Style="{StaticResource Coloring}"><Hyperlink><TextBlock/></HyperLink></TextBlock>

Then both my cases collapse to a TextBlock styling. (In combination with the solution above)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文