您可以使用TextTrimming
a 的属性TextBlock
并创建一个包含 a 的样式TextBlock
,TextBox
如下所示:
样式 1
<!-- Trims text but shows all on-focus -->
<Style TargetType="TextBox" x:Key='TrimmingStyle1'>
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border BorderThickness='1' Background='#ffefefef' BorderBrush='LightBlue'>
<TextBlock Text="{TemplateBinding Text}" TextTrimming="None" Margin='4,1' />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
风格 2
<!-- Trims text always, non editable -->
<Style TargetType="TextBox" x:Key='TrimmingStyle2'>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border BorderThickness='1' Background='#ffefefef' BorderBrush='LightBlue'>
<TextBlock Text="{TemplateBinding Text}" TextTrimming="None" Margin='4,1' />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
用法
<TextBox Style="{StaticResource TrimmingStyle1}" ... />
<TextBox Style="{StaticResource TrimmingStyle2}" ... />
请务必更改文本绑定以匹配您的应用程序数据。另请注意,修剪取决于文本框的大小。
来源:使用样式模拟 TextBox 上的 TextTrimming