文本框显示

文本框正常显示:

文本框超出区域显示:

实现方案

判断文本框是否超出区域

请见《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 文本超出显示时,文本靠右显示的更多相关文章

  1. CSS文本超出2行就隐藏并且显示省略号

    今天做东西,遇到了这个问题,百度后总结得到了这个结果. 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用 ...

  2. css 文本超出n行就隐藏并且显示省略号

    首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; // ...

  3. css文本超出2行就隐藏并显示省略号

    之前在网上看到过这样的代码,感觉有的时候还是挺有用的,故留个笔记. display:-webkit-box; //将对象作为弹性伸缩盒子模型显示. -webkit-box-orient:vertica ...

  4. css 文本超出2行就隐藏并且显示省略号

    overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; -webk ...

  5. android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边

    很纠结的一个标题,实在是不知道怎么去描述这个现象. 上图片吧,先看看需求是什么样的. 1.需求: ​ 视频与票的图标跟在标题后面显示,当标题过长时icon显示到省略号…后(textview省略号显示, ...

  6. CSS3文本超出容器显示省略号之text-overflow属性

    text-overflow:ellipsis; overflow:hidden; white-space:nowrap; 要想实现文本超出容器时显示省略号,上面3个属性必须同时搭配使用

  7. HTML横向滚动条和文本超出显示三个小圆点

    我们这次要说的就是:现在有很多的公司以及很多的app软件经常使用的两个方法横向滚动条和文本超出三个小圆点 横向滚动条:顾名思义嘛,就是能够一块内容可以横着滑动. 文本超出三个小圆点:文本超出就是当文本 ...

  8. CSS控制文本超出指定宽度显示省略号和文本不换行

    一般的文字截断(适用于内联与块): .text-overflow { display:block;/*内联对象需加 */ width:31em;/* 何问起 hovertree.com */ word ...

  9. CSS——文本超出隐藏显示省略号

    文本超出隐藏显示省略号 1.单行文本的溢出显示省略号 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; // overflo ...

随机推荐

  1. [bzoj1088]扫雷

    额,这种水题我也不说什么了233 Description 相信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,“余”人国流行起了一种简单的扫雷游戏,这个 ...

  2. 关于postgresql group by 报错

    举个例子:     table name:makerar cname  | wmname |          avg            --------+-------------+------ ...

  3. [tkinter]为列表框添加滚动条

    为了给列表框配备滚动条,看来很多别人的博客 终于解决了问题 ,现在我总结一下 from tkinter import * root = Tk() lb = Listbox(root) scr = Sc ...

  4. linux 下vim中关于删除某段,某行,或者全部删除的命令 ZZ

    1,先打开某个文件: vim filename 2,转到文件结尾 在命令模式输入 G 3,转到10行 在命令模式输入 10G 4,删除所有内容:先用G 转到文件尾,然后使用下面命令: :1, .d 5 ...

  5. 麒麟子Cocos Creator实用技巧

    大家好,我是麒麟子, 开源棋牌<幼麟棋牌-四川麻将>(泄漏版叫 <达达麻将>)作者,成都幼麟科技创始人. 自09年进入游戏行业以来,不知不觉已经度过了十个春秋. 曾经我也血气方 ...

  6. vs2017开发Node.js控制台程序

    1,新建项目  NodejsConsoleApp1 2,在项目的根目录下,添加 sayModule.js 文件 //sayModule.js function Say1Module() { this. ...

  7. vue父子组件及非父子组件通信

    1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...

  8. [Swift]LeetCode216. 组合总和 III | Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. [Swift]LeetCode326. 3的幂 | Power of Three

    Given an integer, write a function to determine if it is a power of three. Example 1: Input: 27 Outp ...

  10. [Swift]LeetCode672. 灯泡开关 Ⅱ | Bulb Switcher II

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...