本文介绍了如何使用WPF中的样式使textdecoration penOffset工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





大家好......



我基本上试图通过使用TextDecoration的PenOffset属性来改变的方式。如果我在XAML中使用TextDecoration它可以正常工作,但是当我将它应用于样式时,我似乎无法弄清楚为什么它不起作用?



工作代码(下划线显示文本下方5个像素)...





Hi All...

I am basically trying to change the way by using the PenOffset property of TextDecoration. It works fine if I use TextDecoration in XAML, however I cannot seem to figure out why it doesn't work when I apply it to a style??

Working code (underline shows 5 pixels below text)...

<TextBlock>Close
    <TextBlock.TextDecorations>
        <TextDecoration Location="Underline" PenOffset="5" />
    </TextBlock.TextDecorations>
</TextBlock>





当我尝试使用样式做同样的事情时,textdecoration penOffset似乎被忽略了?



注意:应用所有其他风格效果,例如:字体大小,前景等



XAML ...



When I try to use a style to do the same thing the textdecoration penOffset appears to be ignored?

Note: all other style effects are applied e.g. Font Size, Foreground etc

XAML...

<TextBlock Style="{StaticResource closeWindowLink}" >Close</TextBlock>





风格......



Style...

<Style x:Key="closeWindowLink" TargetType="{x:Type TextBlock}">
        <Setter Property="HorizontalAlignment" Value="Right" />
        <Setter Property="Margin" Value="0 3 5 3" />
        <Setter Property="FontStyle" Value="Normal" />
        <Setter Property="TextDecorations" Value="Underline" />
        <Setter Property="TextDecoration.PenOffset" Value="5" />
        <Setter Property="TextDecoration.PenOffsetUnit" Value="Pixel" />
        <Setter Property="TextDecoration.PenThicknessUnit" Value="Pixel" />
        <Setter Property="TextWrapping" Value="NoWrap" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="FontFamily" Value="Verdana" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Foreground" Value="#FFF1CA81" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="#FFFFF1D6" />
            </Trigger>
        </Style.Triggers>
    </Style>

推荐答案

<label margin="5 0 0 0" style="{StaticResource closeWindowLink}">Close</Label></label>





样式(更改为标签并添加带有嵌套TextBlock的模板)...





Style (changed to label and added template with a nested TextBlock)...

<style x:key="closeWindowLink" targettype="{x:Type Label}" xmlns:x="#unknown">
        <setter property="Margin" value="0 3 5 3" />
        <setter property="FontStyle" value="Normal" />
        <setter property="FontSize" value="16" />
        <setter property="FontWeight" value="Bold" />
        <setter property="VerticalAlignment" value="Top" />
        <setter property="FontFamily" value="Verdana" />
        <setter property="SnapsToDevicePixels" value="True" />
        <setter property="Foreground" value="#FFF1CA81" />

        <setter property="Template">
            <setter.value>
                <controltemplate targettype="Label">
                    <textblock>
                        <textblock.textdecorations>
                            <textdecoration location="Underline" penoffset="5" />
                        </textblock.textdecorations>
                        <contentpresenter />
                    </textblock>
                </controltemplate>
            </setter.value>
        </setter>

        <style.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter property="Foreground" value="#FFFFF1D6" />
            </trigger>
        </style.triggers>
    </style>





我相信有一个更优雅的解决方案,而不是将一个TextBlock嵌套在Label中...所以我不会标记为如果有人能提供更好的东西,请回答!



I am sure there is a more elegant solution than having a TextBlock nested in a Label... So I won't mark as answer yet if someone can provide something better!


这篇关于如何使用WPF中的样式使textdecoration penOffset工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:26