I would like to apply a TextTrimming
option on a TextBox
(Not a TextBlock
).
The compiler tells me that the TextTrimming
options is not a valid property of the Textbox
.
I could do a fancy control that is a Textblock
and once it's clicked will become a Textbox
and conversely go back to being a Textblock
once the focus is lost.
Before going this way I would like to know if a built-in function already exists (or is there a smarter way) to allow you to do that?
EDIT: What I want to have in the end is a TextBox
which is trim (the full content will be display in a tooltip) but when the user select the TextBox
(enter in "edit mode") the full content will be display (without trim) therefore the user will be able to modify the full text. when the TextBox
lost the focus (go back to "view mode") the content will be trim again.
Thanks
Try a style like this (I've added background colours to make the change obvious):
<Style TargetType="TextBox"> <Setter Property="Background" Value="Yellow" /> <Style.Triggers> <DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <TextBlock Text="{TemplateBinding Text}" TextTrimming="CharacterEllipsis" Background="Red" /> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style>
Dan Puzey has a great answer, but I wanted to add more so that the style of the TextBlock
appeared like a TextBox
.
Here is the XAML style I came up with:
<Style TargetType="TextBox"> <Style.Triggers> <DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="False"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Border BorderThickness="1" CornerRadius="1"> <Border.BorderBrush> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFABADB3" Offset="0"/> <GradientStop Color="#FFABADB3" Offset="0.044"/> <GradientStop Color="#FFE2E3EA" Offset="0.060"/> <GradientStop Color="#FFE3E9EF" Offset="1"/> </LinearGradientBrush> </Border.BorderBrush> <TextBlock Padding="4,2,0,0" Text="{TemplateBinding Text}" TextTrimming="CharacterEllipsis"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style>
This is what the control looks like when it has no keyboard focus:
This is what the control looks like after gaining keyboard focus:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With