如何使RichTextBox自动调整大小?我希望我的rich textbox能够适应我为其分配的任何文本,而不需要滚动条。谢谢。
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即可。
我认为这会对你有所帮助。
HorizontalAlignment
和VerticalAlignment
设置为除Stretch
(默认值)以外的其他值。这将使TextBox缩小以适应其内容。然后,将其放入具有Orientation
为Horizontal
的StackPanel
中,它将不会垂直滚动,而是被包含面板裁剪,我认为这正是您想要的。如果您希望包含面板根据文本调整大小,则需要相应地配置面板。尝试使用ViewBox
<Viewbox>
<RichTextBox />
</Viewbox>