问题描述
我想在 TextBox
(不是 TextBlock
)上应用 TextTrimming
选项.
I would like to apply a TextTrimming
option on a TextBox
(Not a TextBlock
).
编译器告诉我 TextTrimming
选项不是 Textbox
的有效属性.
The compiler tells me that the TextTrimming
options is not a valid property of the Textbox
.
我可以做一个 Textblock
的奇特控件,一旦点击它就会变成一个 Textbox
并且反过来又回到一个 Textblock
一旦失去焦点.
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?
我最后想要的是一个 TextBox
是修剪的(完整内容将显示在工具提示中)但是当用户选择 TextBox
(进入编辑模式")将显示完整内容(无修剪),因此用户将能够修改全文.当 TextBox
失去焦点(返回查看模式")时,内容将再次被修剪.
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.
谢谢
推荐答案
尝试这样的样式(我添加了背景颜色以使更改显而易见):
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>
这篇关于文本框文本修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!