WPF TextBox/TextBlock 文本超出显示时,文本靠右显示

简介: 文本框显示 文本框正常显示: 文本框超出区域显示:    实现方案 判断文本框是否超出区域 请见《TextBlock IsTextTrimmed 判断文本是否超出》 设置文本布局显示 1.

文本框显示

文本框正常显示:

文本框超出区域显示:

 

 实现方案

判断文本框是否超出区域

请见《TextBlock IsTextTrimmed 判断文本是否超出

设置文本布局显示

1. FlowDirection

当文本超出显示区域时,设置FlowDirection靠右显示

下面是封装的附加属性ScrollEndWhenTextTrimmed

 1         /// <summary>
 2         /// 当文本超出显示时,文本是否靠右侧显示
 3         /// </summary>
 4         public static readonly DependencyProperty ScrollEndWhenTextTrimmedProperty = DependencyProperty.RegisterAttached(
 5         "ScrollEndWhenTextTrimmed", typeof(bool), typeof(TextBoxHelper),
 6         new PropertyMetadata(default(bool), OnScrollEndWhenTextTrimmedChanged));
 7 
 8         private static void OnScrollEndWhenTextTrimmedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 9         {
10             var textBox = (TextBox)d;
11             textBox.TextChanged -= TextBoxOnTextChanged;
12             if ((bool)e.NewValue)
13             {
14                 textBox.FlowDirection = IsTextTrimmed(textBox) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
15                 textBox.TextChanged += TextBoxOnTextChanged;
16             }
17             void TextBoxOnTextChanged(object sender, TextChangedEventArgs args)
18             {
19                 textBox.FlowDirection = IsTextTrimmed(textBox) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
20             }
21         }
22 
23         public static void SetScrollEndWhenTextTrimmed(DependencyObject element, bool value)
24         {
25             element.SetValue(ScrollEndWhenTextTrimmedProperty, value);
26         }
27 
28         public static bool GetScrollEndWhenTextTrimmed(DependencyObject element)
29         {
30             return (bool)element.GetValue(ScrollEndWhenTextTrimmedProperty);
31         }

 

在需要设置文本超出时居右显示的TextBox控件中,添加附加属性ScrollEndWhenTextTrimmed即可。

2.ScrollToEnd

类似方案FlowDirection,文本超出时,通过滚动到文本末尾后,文本靠右显示。

如方案FlowDirection,可以在添加附加属性更改事件中,订阅TextBox的TextChanged。

1     textBox.SelectionStart = textBox.Text.Length;
2     textBox.ScrollToEnd();

But,此方案有缺陷。当TextBox设置IsEnabled=false时,就无法滚动到了。即使如下设置依然无效:

1     textBox.IsEnabled = true;
2     textBox.SelectionStart = textBox.Text.Length;
3     textBox.ScrollToEnd();
4     textBox.IsEnabled = false;

当然,如果文本框不设置IsEnabled时,此方案是可行的。

注:如上方案,本来通过SelectionStart直接绑定TextBox自身的Text.Length就行。然而SelectionStart不是依赖属性,只能直接赋值~

 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
目录
相关文章
|
8月前
|
数据可视化 数据处理 C#
WPF技术之TextBox控件
WPF ProgressBar控件用于显示操作的进度。它提供了一个可视化的进度条,用于表示任务的完成程度
70 0
|
8月前
|
C# Windows
WPF技术之TextBlock控件
WPF(Windows Presentation Foundation)的TextBlock控件是用于显示文本的控件。与Label控件相比,TextBlock提供了更多的灵活性和格式化选项。
321 1
|
8月前
|
C# 数据安全/隐私保护 Windows
WPF技术之TextBox控件
WPF(Windows Presentation Foundation)的TextBox控件是用于用户输入和编辑文本的常见控件
634 0
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
|
C#
wpf 开发 -TextBox背景自定义-Decorator
首先在app.xaml文件的下面添加以下样式
1630 0
|
C#
【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?
原文:【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/148e95c6-6fb5-4399-8a56-4...
945 0
|
C#
【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
原文:【msdn wpf forum翻译】TextBox中文本 中对齐 的方法原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558 问题:TextBox中文本中对齐,使用 TextBox.HorizontalContentAlignment="Center"行不通(TextBox.VerticalContentAlignment="Center"则会起到预期的作用。
1197 0
|
C#
WPF控件TextBlock中文字自动换行
原文:WPF控件TextBlock中文字自动换行 在很多的WPF项目中,往往为了追求界面的美观,需要控制控件中文字的换行显示,现对TextBlock控件换行的实现方式进行总结,希望大家多多拍砖!!! 1.
2764 0
|
C#
WPF TextBlock IsTextTrimmed 判断文本是否超出
原文:WPF TextBlock IsTextTrimmed 判断文本是否超出 WPF TextBlock 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号)     private bool IsTextTrimmed(Te...
1243 0