设置WPF RichTextBox的宽度和高度根据等宽字体的大小 [英] Setting WPF RichTextBox width and height according to the size of a monospace font

查看:701
本文介绍了设置WPF RichTextBox的宽度和高度根据等宽字体的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以适应WPF的RichTextBox准确容纳字符的网格,尤其是等宽字体。我目前使用FormattedText以确定我的RichTextBox的宽度和高度,但它为我提供测量太小 - 宽度太小,特别是两个字符



有没有执行此任务的更好的办法?这似乎并不成为一个合适的方式来确定我的控件的大小

  RichTextBox的RTB; 
RTB =新的RichTextBox();
的FontFamily fontFamily中=新的FontFamily(索拉);
双fontSize的= 16;
字符standardizationCharacter ='X';
字符串standardizationLine =;
为(长循环= 0;环路LT;列;环++){
standardizationLine + = standardizationCharacter;
}
standardizationLine + = Environment.NewLine;
字符串standardizationString =;
为(长循环= 0;环路LT;行;环++){
standardizationString + = standardizationLine;
}
字样字体=新的字体(fontFamily中,FontStyles.Normal,FontWeights.Normal,FontStretches.Normal);
FormattedText formattedText =新FormattedText(standardizationString,CultureInfo.CurrentCulture,FlowDirection.LeftToRight,字体,fontSize的,Brushes.Black);
rtb.Width = formattedText.Width;
rtb.Height = formattedText.Height;


解决方案

假设你有一个非常基本的 RichTextBox的只标准的FlowDocument 里面,那么你必须考虑到所有的额外布局的RichTextBox 使用。



Document.PagePadding 属性默认为 {5,0,5,0} 。所以,你必须添加10的宽度。



此外在的RichTextBox 有一个了borderThickness ,默认值为 {1,1,1,1} 。所以你必须加2,宽度



所以,你想可能看起来像代码:

  VAR pagePadding = rtb.Document.PagePadding; 
VAR了borderThickness = rtb.BorderThickness;
rtb.Width = formattedText.Width
+ pagePadding.Left + pagePadding.Right
+ borderThickness.Left + borderThickness.Right;

如果你这样做,你将仍然由1个字符被关闭,但是这是一个有点误导。在的RichTextBox 如果文本只是一点点宽而不是整个字符宽度甚至会换行。如果添加2宽度它的作品了。其中,额外2是来自我无法弄清楚到底。我从来没有试图让宽度是如此精确。我已经运行到 PagePadding 了borderThickness 问题之前,但我只是找不到多余的2。它可能只是你被卡住恒定的,但我对此表示怀疑。它在什么地方。



一个小技巧上产生 StandardizationLine ,你可以做

 字符串StandardizationLine =新的字符串('X',列); 

要清理循环。


I am trying to fit a WPF RichTextBox to exactly accommodate a grid of characters in a particular monospace font. I am currently using FormattedText to determine the width and height of my RichTextBox, but the measurements it is providing me with are too small--specifically two characters in width too small.

Is there a better way to perform this task? This does not seem to be an appropriate way to determine the size of my control.

RichTextBox rtb;
rtb = new RichTextBox();
FontFamily fontFamily = new FontFamily("Consolas");
double fontSize = 16;
char standardizationCharacter = 'X';
String standardizationLine = "";
for(long loop = 0; loop < columns; loop ++) {
    standardizationLine += standardizationCharacter;
}
standardizationLine += Environment.NewLine;
String standardizationString = "";
for(long loop = 0; loop < rows; loop ++) {
    standardizationString += standardizationLine;
}
Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText(standardizationString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
rtb.Width = formattedText.Width;
rtb.Height = formattedText.Height;

解决方案

Assuming you have a very basic RichTextBox with just the standard FlowDocument inside it then you have to take into account all of the extra layout RichTextBox uses.

The Document.PagePadding property defaults to {5,0,5,0}. So you will have to add 10 to the width.

Additionally the RichTextBox has a BorderThickness that defaults to {1, 1, 1, 1}. So you will have to add 2 to the width.

So the code you want could look like:

var pagePadding = rtb.Document.PagePadding;
var borderThickness = rtb.BorderThickness;
rtb.Width = formattedText.Width
    + pagePadding.Left + pagePadding.Right 
    + borderThickness.Left + borderThickness.Right;

If you do that you will still be off by 1 character, but that's a bit misleading. The RichTextBox will wrap even if the text is just a tiny bit to wide not just a whole character width. If you add 2 to the width it works out. I can't figure out exactly where that extra 2 is coming from. I've never tried to get the width to be so exact. I have run into the PagePadding and BorderThickness issues before, but I just can't find the extra 2. It might just be a constant you are stuck with, but I doubt it. It's got to be somewhere.

A small tip on producing the StandardizationLine you can do

string StandardizationLine = new string('X', columns);

to clean up the loop.

这篇关于设置WPF RichTextBox的宽度和高度根据等宽字体的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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