如何将样式应用于不同的“TextBlock”对象?

杰米斯特

这是我的应用程序 ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CABI_PO_Manager.Themes">   
 
    <Style TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="Comic Sans MS"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>


    <Style BasedOn="{StaticResource {x:Type TextBlock}}"
           TargetType="TextBlock"
           x:Key="YellowTextBlock">
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Foreground" Value="#d8b243"/>
    </Style>

    <Style BasedOn="{StaticResource {x:Type TextBlock}}"
           TargetType="TextBlock"
           x:Key="GreenTextBlock">
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="Foreground" Value="Green"/>
    </Style>

    <Style BasedOn="{StaticResource {x:Type TextBlock}}"
           TargetType="TextBlock"
           x:Key="RedTextBlock">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Foreground" Value="#a01e21"/>
    </Style>
</ResourceDictionary>

我想有一些默认样式TextBlock可以工作

    <Style TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="Comic Sans MS"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>

我找到了可以使用的地方,x:Key但无法正常工作。

我将有几个TextBlock,如何TextBlock将 UI XAML 中的a 标识为红色、黄色或绿色TextBlock并将该样式应用于它们?这不被认可x:Key="GreenTextBlock"

<TextBlock x:Key="GreenTextBlock"  Grid.Column="1" Margin="0,10,0,0" TextWrapping="Wrap" Text="PO Manager" VerticalAlignment="Top" TextAlignment="Center" FontWeight="ExtraBold"/>
沃科迪

如果你想在应用程序中的所有 TextBlocks 上应用样式,只需使用没有 x:key 定义的样式,例如

  <Style TargetType="{x:Type TextBlock}">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="Comic Sans MS"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>

应用样式时,请使用TargetType="{x:Type TextBlock}"而不是 TargetType="TextBlock"

你什么时候使用想基于其他风格使用

BaseOn="{StaticResource StyleUWantToBaseOn}"

其中 StyleUWantToBaseOn 是带有 x:Key 属性的样式

当您想在文本块上应用特定样式时,您要使用 Style 属性,例如:

<TextBlock Style="{StaticResource GreenTextBlock}" Grid.Column="1" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章