如何使一个RichTextBox自动调整大小? 我希望我的丰富文本框,以配合而无需滚动条我分配给它的任何文本。 谢谢。
Answer 1:
设置HorizontalAlignment
和VerticalAlignment
比其它东西Stretch
(这是默认值)。 这将使文本框缩小以适合其内容。 如果你然后坚持它内部StackPanel
与Orientation
的Horizontal
也不会垂直滚动,但它包含的面板,我想这是你所追求被裁剪。 如果你想你包含面板本身调整的文字,那么你就需要相应地配置面板
Answer 2:
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
// Create the Font object for the image text drawing.
Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);
// This is where the bitmap size is determined.
intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
你已经有了字符串设置文本框。 首先,你可以计算像上面来衡量的RichTextBox的适当大小。 你可以设置intWidth和intHeight在文本框。 我想,让你有帮助。
Answer 3:
尝试使用视框
<Viewbox>
<RichTextBox />
</Viewbox>
文章来源: How to make a rich textbox automatically size in wpf?