开发者

Silverlight: Button template with texttrimming cut off

开发者 https://www.devze.com 2023-01-31 17:31 出处:网络
I\'m replacing the default Button template\'s ContentPresenter with a TextBlock, so the text can be trimmed when it\'s too long.

I'm replacing the default Button template's ContentPresenter with a TextBlock, so the text can be trimmed when it's too long.

Wor开发者_如何转开发ks fine in WPF. In Silverlight the text gets pushed to one edge and cut off on the left, even when there's space on the right:

Silverlight: Button template with texttrimming cut off

Template is nothing special, just replaced the ContentPresenter with the TextBlock:

            <Border x:Name="bdrBackground" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            Background="{TemplateBinding Background}" />

        <Rectangle x:Name="rectMouseOverVisualElement"
            Opacity="0">
            <Rectangle.Fill>
                <SolidColorBrush x:Name="rectMouseOverColor" 
                    Color="{StaticResource MouseOverItemBgColor}"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle x:Name="rectPressedVisualElement" 
            Opacity="0" 
            Style="{TemplateBinding Tag}"/>

        <TextBlock x:Name="textblock" 
            Text="{TemplateBinding Content}" 
            TextTrimming="WordEllipsis"
            TextWrapping="NoWrap"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            Margin="{TemplateBinding Padding}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>


        <Rectangle x:Name="rectDisabledVisualElement" 
            Opacity="0" 
            Style="{StaticResource RectangleDisabledStyle}"/>

        <Rectangle x:Name="rectFocusVisualElement" 
            Opacity="0" 
            Style="{StaticResource RectangleFocusStyle}"/>              

    </Grid>
</ControlTemplate>  

How do I fix this?


More info: With the latest comment about HorizontalAlignment, it's clear that SL's implementation of TextTrimming differs from WPF's. In SL, TextTrimming only really works if the text is aligned left. SL isn't smart enough to align the text the way WPF does. For instance:

WPF button:

Silverlight: Button template with texttrimming cut off

SL button with the textblock horizontalalignment = left:

Silverlight: Button template with texttrimming cut off

SL button with textblock horizontalalignment = center:

Silverlight: Button template with texttrimming cut off


There's an even simpler solution. I set the TextBlock's TextAlignment=Center. Works exactly like in WPF. Thanks for the help!


The problem will be that your HorizontalContentAlignment is set to "Center". Really WordEllipsis only makes sense when the HorizontalAlignment of the TextBlock is set to "Left".

Edit

Getting the desired behaviour try this:-

<Border HorizontalAlignment="Center"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
    <TextBlock x:Name="textblock"
        Text="{TemplateBinding Content}"
        TextTrimming="WordEllipsis"
        TextWrapping="NoWrap"
        Margin="{TemplateBinding Padding}" />
</Border>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号