Silverlight: button template with texttrimming disabled

I am replacing the default Button Button ContentPresenter template with a TextBlock, so the text can be cut off when it is too long.

Works great in WPF. In Silverlight, text is pushed to one edge and trimmed to the left, even if there is a space to the right:

alt text

The template is nothing special, just replaced ContentPresenter with 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 to fix it?


Additional Information: With the last comment on HorizontalAlignment, it is clear that the implementation of SL TextTrimming is different from WPF. In SL, TextTrimming only works if the text is left justified. SL is not smart enough to align text like WPF does. For instance:

WPF Button:

alt text

SL button with horizontal alignment of the text block = left:

alt text

SL = :

alt text

+3
2

. TextBlock TextAlignment = . , WPF. !

+3

, HorizontalContentAlignment "". WordEllipsis , HorizontalAlignment TextBlock "Left".

Edit

: -

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

Source: https://habr.com/ru/post/1781766/


All Articles