如何使用WPF中的样式使textdecoration penOffset工作? [英] How do I get textdecoration penOffset to work using a style in WPF?

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

问题描述

Textdecoration在XAML中有效但不在样式中吗?



大家好......



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



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



Textdecoration works in XAML but not in a Style?

Hi All...

I am basically trying to change the way underline works 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和TEXTBLOCK之间存在很大差异...请参阅这里



我认为我有什么不起作用,因为有没有ContentPresenter或更改模板的能力所以我将TEXTBLOCK更改为LABEL并应用了一个控件模板(现在可以正常工作)。



这就是我做的......



XAML(将TextBlock更改为标签)......



Don't like to answer my own question but have found an answer/workaround...

It seems that there is a big difference between a LABEL and a TEXTBLOCK... see here

I think what I have does not work because there is no ContentPresenter or ability to change the template so I changed the TEXTBLOCK to a LABEL and applied a control template (which now works).

This is what I did...

XAML (changed TextBlock to Label)...

<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工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆