WPF TextBox/TextBlock 文本超出显示时,文本靠右显示
文本框显示
文本框正常显示:

文本框超出区域显示:

实现方案
判断文本框是否超出区域
请见《TextBlock IsTextTrimmed 判断文本是否超出》
设置文本布局显示
1. FlowDirection
当文本超出显示区域时,设置FlowDirection靠右显示
下面是封装的附加属性ScrollEndWhenTextTrimmed
/// <summary>
/// 当文本超出显示时,文本是否靠右侧显示
/// </summary>
public static readonly DependencyProperty ScrollEndWhenTextTrimmedProperty = DependencyProperty.RegisterAttached(
"ScrollEndWhenTextTrimmed", typeof(bool), typeof(TextBoxHelper),
new PropertyMetadata(default(bool), OnScrollEndWhenTextTrimmedChanged)); private static void OnScrollEndWhenTextTrimmedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = (TextBox)d;
textBox.TextChanged -= TextBoxOnTextChanged;
if ((bool)e.NewValue)
{
textBox.FlowDirection = IsTextTrimmed(textBox) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
textBox.TextChanged += TextBoxOnTextChanged;
}
void TextBoxOnTextChanged(object sender, TextChangedEventArgs args)
{
textBox.FlowDirection = IsTextTrimmed(textBox) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
}
} public static void SetScrollEndWhenTextTrimmed(DependencyObject element, bool value)
{
element.SetValue(ScrollEndWhenTextTrimmedProperty, value);
} public static bool GetScrollEndWhenTextTrimmed(DependencyObject element)
{
return (bool)element.GetValue(ScrollEndWhenTextTrimmedProperty);
}
在需要设置文本超出时居右显示的TextBox控件中,添加附加属性ScrollEndWhenTextTrimmed即可。
2.ScrollToEnd
类似方案FlowDirection,文本超出时,通过滚动到文本末尾后,文本靠右显示。
如方案FlowDirection,可以在添加附加属性更改事件中,订阅TextBox的TextChanged。
textBox.SelectionStart = textBox.Text.Length;
textBox.ScrollToEnd();
But,此方案有缺陷。当TextBox设置IsEnabled=false时,就无法滚动到了。即使如下设置依然无效:
textBox.IsEnabled = true;
textBox.SelectionStart = textBox.Text.Length;
textBox.ScrollToEnd();
textBox.IsEnabled = false;
当然,如果文本框不设置IsEnabled时,此方案是可行的。
注:如上方案,本来通过SelectionStart直接绑定TextBox自身的Text.Length就行。然而SelectionStart不是依赖属性,只能直接赋值~

WPF TextBox/TextBlock 文本超出显示时,文本靠右显示的更多相关文章
- CSS文本超出2行就隐藏并且显示省略号
今天做东西,遇到了这个问题,百度后总结得到了这个结果. 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用 ...
- css 文本超出n行就隐藏并且显示省略号
首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; // ...
- css文本超出2行就隐藏并显示省略号
之前在网上看到过这样的代码,感觉有的时候还是挺有用的,故留个笔记. display:-webkit-box; //将对象作为弹性伸缩盒子模型显示. -webkit-box-orient:vertica ...
- css 文本超出2行就隐藏并且显示省略号
overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; -webk ...
- android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边
很纠结的一个标题,实在是不知道怎么去描述这个现象. 上图片吧,先看看需求是什么样的. 1.需求: 视频与票的图标跟在标题后面显示,当标题过长时icon显示到省略号…后(textview省略号显示, ...
- CSS3文本超出容器显示省略号之text-overflow属性
text-overflow:ellipsis; overflow:hidden; white-space:nowrap; 要想实现文本超出容器时显示省略号,上面3个属性必须同时搭配使用
- HTML横向滚动条和文本超出显示三个小圆点
我们这次要说的就是:现在有很多的公司以及很多的app软件经常使用的两个方法横向滚动条和文本超出三个小圆点 横向滚动条:顾名思义嘛,就是能够一块内容可以横着滑动. 文本超出三个小圆点:文本超出就是当文本 ...
- CSS控制文本超出指定宽度显示省略号和文本不换行
一般的文字截断(适用于内联与块): .text-overflow { display:block;/*内联对象需加 */ width:31em;/* 何问起 hovertree.com */ word ...
- CSS——文本超出隐藏显示省略号
文本超出隐藏显示省略号 1.单行文本的溢出显示省略号 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; // overflo ...
随机推荐
- IDEA使用Git传放项目
使用Git下载项目到IDEA工具上开发 1. 下载Git 软件工具 https://git-scm.com/ 2. 下载安装 3.打开IDEA 配置Git 4. 搜索Git 在登入 5.选择自己Git ...
- docker 安装 zookeeper
镜像下载hub.docker.com 上有不少 ZK 镜像, 不过为了稳定起见, 我们就使用官方的 ZK 镜像吧.首先执行如下命令: docker pull zookeeper当出现如下结果时, 表示 ...
- Get API
根据索引.类型和ID获取文档 GET twitter/_doc/1 返回结果如下: { "_index": "twitter", "_type&quo ...
- async/await 的理解
1.如果一个方法标记了 async 关键字,那么这个方法被调用时就是异步执行: 2.利用Task运行一个任务,这个任务里的函数也是异步执行: 3.如果一个任务前被标记await,那么等待这个任务执行完 ...
- 【安富莱二代示波器教程】第19章 附件E---参考资料
第19章 附件E---参考资料 DSP教程 http://forum.armfly.com/forum.php?mod=viewthread&tid=3886 . FreeRTOS教 ...
- [Swift]LeetCode161. 一次编辑距离 $ One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串S和T,确定它们是否都是是一 ...
- [Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 英语笔记3(git)
一: To create a new branch and switch to it at the same time, you can run the git checkout command wi ...