特定の等幅フォントの文字のグリッドを正確に収容するために、WPF RichTextBox を適合させようとしています。現在、RichTextBox の幅と高さを決定するために FormattedText を使用していますが、提供される測定値が小さすぎます。具体的には、幅が 2 文字小さすぎます。
このタスクを実行するためのより良い方法はありますか? これは、コントロールのサイズを決定する適切な方法ではないようです。
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;