今天遇到一个需求,界面文本框(textbox)设置固定宽度,如果输入的字符超过文本框长度且焦点离开的时候,超出的显示为省略号,再次获得焦点的时候把文本还原,因为是初学者,开始一脸懵逼的想了很久,甚至用转换器转换字符,都达不到要求,最后发现只需要在style里加个textbook,使用它自带的属性:TextTrimming 就可以了。
style:
<Window.Resources>
<SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3" />
<SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA" />
<SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5" />
<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}" />