如何为Label设置TextBlock样式?

时间:2017-10-05 12:06:33

标签: wpf xaml

Style Label <Application.Resources>就像<Style x:Key="HeaderBar" TargetType="{x:Type Label}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Label}"> <Grid> <Rectangle x:Name="TitleBox" Fill="#FFECDC54"> <Rectangle.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <LinearGradientBrush.RelativeTransform> <TransformGroup> <ScaleTransform CenterY="0.5" CenterX="0.5"/> <SkewTransform CenterY="0.5" CenterX="0.5"/> <RotateTransform Angle="140" CenterY="0.5" CenterX="0.5"/> <TranslateTransform/> </TransformGroup> </LinearGradientBrush.RelativeTransform> <GradientStop Color="#FFA09F9F" Offset="0.4"/> <GradientStop Color="White" Offset="0.5"/> </LinearGradientBrush> </Rectangle.Stroke> </Rectangle> <ContentPresenter HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 这样定义{/ 1}}:

<Style x:Key="TextStyleMedium" TargetType="{x:Type TextBlock}">
    <Setter Property="TextWrapping" Value="NoWrap"/>
    <Setter Property="TextTrimming" Value="None"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="FontFamily" Value="Franklin Gothic Medium Cond"/>
</Style>

我还为TextBlocks定义了一种样式:

cellForRowAt

如何将Label设置为使用TextBlock样式(“TextStyleMedium”)作为Label中的文本?

3 个答案:

答案 0 :(得分:3)

您可以对框架元素使用通用基本样式,如下所示:

    <Style x:Key="TextBase" TargetType="{x:Type FrameworkElement}">
        <Setter Property="TextBlock.TextWrapping" Value="NoWrap"/>
        <Setter Property="TextBlock.TextTrimming" Value="None"/>
        <Setter Property="TextBlock.FontSize" Value="16"/>
        <Setter Property="TextBlock.FontFamily" Value="Franklin Gothic Medium Cond"/>
    </Style>

    <Style x:Key="TextStyleMedium" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBase}">
    </Style>

    <Style x:Key="HeaderBar" TargetType="{x:Type Label}" BasedOn="{StaticResource TextBase}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Grid>
                        <Rectangle x:Name="TitleBox" Fill="#FFECDC54">
                            <Rectangle.Stroke>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <LinearGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                                            <SkewTransform CenterY="0.5" CenterX="0.5"/>
                                            <RotateTransform Angle="140" CenterY="0.5" CenterX="0.5"/>
                                            <TranslateTransform/>
                                        </TransformGroup>
                                    </LinearGradientBrush.RelativeTransform>
                                    <GradientStop Color="#FFA09F9F" Offset="0.4"/>
                                    <GradientStop Color="White" Offset="0.5"/>
                                </LinearGradientBrush>
                            </Rectangle.Stroke>
                        </Rectangle>
                        <ContentPresenter HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

答案 1 :(得分:1)

  

如何为Label中的文字设置TextBlock使用Label样式(&#34; TextStyleMedium&#34;)?

Content的{​​{1}}设置为您应用自己风格的Label

TextBlock

<Label Style="{StaticResource HeaderBar}"> <Label.Content> <TextBlock Text="text..." Style="{StaticResource TextStyleMedium}" /> </Label.Content> </Label> 没有,默认情况下不会创建Label。它是一个TextBlock,其ContentControl属性可以设置为您想要的任何内容,包括Content或样式string

答案 2 :(得分:0)

这样做的一种方法是将其作为隐式样式包含在<Label Content="ABC"> <Label.Template> <ControlTemplate TargetType="{x:Type Label}"> <Grid> <Rectangle x:Name="TitleBox" Fill="#FFECDC54"> <Rectangle.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <LinearGradientBrush.RelativeTransform> <TransformGroup> <ScaleTransform CenterY="0.5" CenterX="0.5"/> <SkewTransform CenterY="0.5" CenterX="0.5"/> <RotateTransform Angle="140" CenterY="0.5" CenterX="0.5"/> <TranslateTransform/> </TransformGroup> </LinearGradientBrush.RelativeTransform> <GradientStop Color="#FFA09F9F" Offset="0.4"/> <GradientStop Color="White" Offset="0.5"/> </LinearGradientBrush> </Rectangle.Stroke> </Rectangle> <ContentPresenter HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <ContentPresenter.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="TextWrapping" Value="NoWrap"/> <Setter Property="TextTrimming" Value="None"/> <Setter Property="FontSize" Value="32"/> <Setter Property="FontFamily" Value="Franklin Gothic Medium Cond"/> </Style> </ContentPresenter.Resources> </ContentPresenter> </Grid> </ControlTemplate> </Label.Template> </Label> 资源中。像这样:

TextBlock

这会将您的样式应用于生成的{{1}}元素。