如何将所有文本块元素定义为相同的颜色

发布于 2024-12-12 04:09:44 字数 296 浏览 0 评论 0原文

我们对大多数类型使用全局样式定义。然后我们在 app.xaml 文件中定义。使用 TextBlock 时,定义前景色是一个问题,因为它会更改使用 TextBlock 的所有控件(例如按钮的内容颜色)。 我们如何定义一个仅作用于特定 TextBlock 用法的全局样式?

当前有问题的用法:

<Style TargetType={x:Type TextBlock}>
  <Setter Property="Foreground" Value="Red"/>
</Style>

We are using global styles definitions for most of the types. We define then in the app.xaml file. When using TextBlock it is a problem to define a foreground color because it changes all the controls using TextBlock (Button's content color for example).
How can we define a global style which will act only on specific TextBlock usages?

current problematic usage:

<Style TargetType={x:Type TextBlock}>
  <Setter Property="Foreground" Value="Red"/>
</Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

余生再见 2024-12-19 04:09:44

由于我认为没有办法区分“您的”TextBlock 和其他控件的一部分,因此您的选择非常有限。

  • 您可以创建命名的 Style 并将 Style="{StaticResource ColoredTextBlock}"Foreground="{StaticResource textBlockColor}" 添加到所有 TextBlock。这将是相当乏味且不DRY的。
  • 您可以创建自己的继承自 TextBlock 的类型并设置其样式。这具有上述解决方案的一些缺点(您必须记住这样做)。但它的重复次数要少得多。

Since I don't think there is a way to differentiate “your” TextBlocks and those that are part of other controls, your options are quite limited.

  • You could create named Style and add Style="{StaticResource coloredTextBlock}" or Foreground="{StaticResource textBlockColor}" to all TextBlocks. This would be quite tedious and non-DRY.
  • You could create your own type that inherits from TextBlock and style that. This has some of the disadvantages of the above solution (you have to remember doing that). But it has much less repetition.
喜你已久 2024-12-19 04:09:44

这是因为 ContentPresenter 为字符串内容创建一个 TextBlock,并且由于该 TextBlock 不在可视化树中,因此它将查找应用程序级别资源。如果您在应用程序级别为 TextBlock 定义样式,那么它将应用于 ControlControls 内的这些 TextBlock。

解决方法是为 System.String 定义一个 DataTemplate,我们可以在其中显式使用默认的 TextBlock 来显示内容。您可以将该 DataTemplate 放置在您定义 TextBlock 样式的同一字典中,以便该 DataTemplate 将应用于受您的样式影响的任何 ContentPresenter。

将其添加到您的应用程序资源中,它应该适合您 -

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
    <TextBlock.Resources>
      <Style TargetType="{x:Type TextBlock}"/>
    </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

在您的 xaml 中声明一个命名空间(如果尚未引用) -

xmlns:system="clr-namespace:System;assembly=mscorlib"

编辑:检查此示例的工作位置..

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" Value="Red"/>
</Style>

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
     <TextBlock.Resources>
        <Style TargetType="{x:Type TextBlock}"/>
     </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

<Style TargetType="{x:Type Button}">
  <Setter Property="Foreground" Value="Yellow"/>
</Style>

<Style TargetType="{x:Type Label}">
  <Setter Property="Foreground" Value="Blue"/>
</Style>

This is because ContentPresenter creates a TextBlock for a string content, and since that TextBlock isn't in the visual tree, it will lookup to Application level resource. And if you define a style for TextBlock at Application level, then it will be applied to these TextBlock within ControlControls.

A workaround is to define a DataTemplate for System.String, where we can explicitly use a default TextBlock to display the content. You can place that DataTemplate in the same dictionary you define the TextBlock style so that this DataTemplate will be applied to whatever ContentPresenter effected by your style.

Add this to your Application resources and it should work for you -

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
    <TextBlock.Resources>
      <Style TargetType="{x:Type TextBlock}"/>
    </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

Declare a namespace in your xaml, if not referred already -

xmlns:system="clr-namespace:System;assembly=mscorlib"

EDIT : Check this sample where its working..

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" Value="Red"/>
</Style>

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
     <TextBlock.Resources>
        <Style TargetType="{x:Type TextBlock}"/>
     </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

<Style TargetType="{x:Type Button}">
  <Setter Property="Foreground" Value="Yellow"/>
</Style>

<Style TargetType="{x:Type Label}">
  <Setter Property="Foreground" Value="Blue"/>
</Style>
最单纯的乌龟 2024-12-19 04:09:44

只需在样式中提供 ax:key ,例如:

<Style x:Key="stRedTextBlock" TargetType={x:Type TextBlock}>
        <Setter Property="Foreground" Value="Red"/>
</Style>

并在 TextBlock 控件样式中提及该键,无论您何时需要此特定的 TextBlock 样式,例如:

<TextBlock Name="textBlock1" Style="{StaticResource stRedTextBlock}" />

Just provide a x:key in the style, like:

<Style x:Key="stRedTextBlock" TargetType={x:Type TextBlock}>
        <Setter Property="Foreground" Value="Red"/>
</Style>

and mention the key in the TextBlock control style, where ever you require this particular TextBlock style, like:

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