Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox TextTrimming

Tags:

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

like image 767
Guillaume Avatar asked Apr 19 '12 11:04

Guillaume


2 Answers

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> 
like image 109
Dan Puzey Avatar answered Oct 12 '22 23:10

Dan Puzey


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:

TextBlock - No Keyboard Focus

This is what the control looks like after gaining keyboard focus:

TextBox - With Keyboard Focus

like image 39
Nicholas Miller Avatar answered Oct 12 '22 23:10

Nicholas Miller