Add ellipsis in placeholder UWP

Anderson Rodrigues Cavalcante 271 Reputation points
2024-03-14T21:17:24.3+00:00

Hi everyone.
I have a textbox with a placeholder and a button inside. I'd like to add an ellipsis at the end of the placeholder text but before the button. If the placeholder text is longer than the area available, the ellipsis must be added. The image attached shows the placeholder and a red button. The ellipsis must be on the left side of the button.
Obs: I have to deal with different languages and different letter symbols.
User's image

The expected result in the perfect world:
User's image

or something like this, disregarding the button:
User's image

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 14,751 Reputation points Microsoft Vendor
    2024-03-15T03:27:05.9833333+00:00

    Hi @Anderson Rodrigues Cavalcante .

    Welcome to Microsoft Q&A!

    or something like this, disregarding the button: User's image

    For this effect, you only need to add TextTrimming="CharacterEllipsis" to the PlaceholderTextContentPresenter in the Textbox style.

    1.Open the xaml designer, find your Textbox, right click it.User's image

    2.Select Edit Template -> Edit a Copy.

    User's image

    3.Name it and click OK.

    4.Find <TextBlock x:Name="PlaceholderTextContentPresenter"... , add TextTrimming="CharacterEllipsis"

    5.Use this new style in your TextBox.

    ...
    <TextBlock x:Name="PlaceholderTextContentPresenter" 
               Grid.ColumnSpan="2" Foreground="{Binding PlaceholderForeground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource TextControlPlaceholderForeground}}" 
               IsHitTestVisible="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" 
               Grid.Row="1" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" 
               Text="{TemplateBinding PlaceholderText}"
               TextTrimming="CharacterEllipsis"/>
    ...
    <TextBox Style="{StaticResource TextBoxPlaceholderStyle}" PlaceholderText="A text box that demonstrates TextWrapping, TextAlignment, MaxLength, and AcceptsReturn"  
     Width="300"/>
    

    Could you please provide the code of how you add a button to the textbox? Maybe I can help you achieve the expected effect.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful