WPF文本框文本对齐问题 [英] WPF Textbox text align question

查看:119
本文介绍了WPF文本框文本对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在textbox或richtextbox中对齐文本,如下所示:

 word:1 
word2:2
word3:3
word4:4
superlongword:5
otherword:6
abc:7



i试图在单词后面添加制表符号,但是如果所有单词都有相同的长度,这个效果很好。

任何想法?

解决方案

请看我对这个问题的评论。 />


这是你在XAML中的方法: http://www.wpf-tutorial.com/rich-text-controls/richtextbox-control [ ^ ]。



即时格式化文档的某些部分,你需要获得一些文本范围, System.Windows.Documents.TextRange https://msdn.microsoft.com/en-us/library/system.windows.documents .textrange%28v = vs.110%29.aspx [ ^ ]。



从继承图中可以看出,它可以是,例如,选择。



现在你可以为范围应用一些格式(这可能适用与否;在你的情况下,它必须是一些块元素,如段落)。你会在这里找到方法: https://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.applypropertyvalue%28v=vs.110%29.aspx [ ^ ]。



查看最后的代码示例。



-SA

How i can align text in textbox or richtextbox like this:

word:             1
word2:            2
word3:            3
word4:            4
superlongword:    5
otherword:        6
abc:              7


i tried to add tabulation symbol after word, but this is working well, if all words have same length.
any ideas?

解决方案

Please see my comment to the question.

This is how you can do it in XAML: http://www.wpf-tutorial.com/rich-text-controls/richtextbox-control[^].

To format some part of the document on the fly, you need to obtain some text range, System.Windows.Documents.TextRange: https://msdn.microsoft.com/en-us/library/system.windows.documents.textrange%28v=vs.110%29.aspx[^].

As you can see from the inheritance diagram, it can be, for example, selection.

Now you can apply some formatting for the range (which can be applicable or not; in your case, it has to be some block element, such as a paragraph). You will find the way to do it here: https://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.applypropertyvalue%28v=vs.110%29.aspx[^].

Look at the code sample at the end.

—SA


这篇关于WPF文本框文本对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆