如何在wpf中自动设置丰富的文本框大小?

时间:2021-02-03 00:00:10

How to make a RichTextBox automatically size? I want my rich textbox to fit with any text I assign to it without having scroll bars. Thanks.

如何使一个RichTextBox自动大小?我想要我的富文本框适合任何我分配给它而没有滚动条的文本。谢谢。

3 个解决方案

#1


1  

Set the HorizontalAlignment and VerticalAlignment to something other than Stretch (which is the default). This will make the TextBox shrink to fit its contents. If you then stick it inside a StackPanel with Orientation of Horizontal it will not scroll vertically but be clipped by its containing panel, which I think is what you are after. If you want you containing panel to resize itself to the text, then you need to configure the panel accordingly

将水平对齐和垂直对齐设置为非拉伸(默认)。这将使文本框缩小以适应其内容。如果你把它放在一个水平方向的堆叠板里它不会垂直滚动,而是被它的嵌板夹住,我想这就是你想要的。如果您希望包含面板来调整文本大小,则需要相应地配置面板

#2


0  

        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;

You already have string to set textbox. Firstly, you can calculate like above to measure proper size of richtextbox. you can just set intWidth and intHeight at textbox. I think that makes you helpful.

您已经有了要设置文本框的字符串。首先,你可以像上面那样计算来测量richtextbox的大小。你可以设置文本框的宽度和宽度。我觉得这对你有帮助。

#3


-1  

Try using the ViewBox

尝试使用ViewBox

<Viewbox>
    <RichTextBox />
</Viewbox>

#1


1  

Set the HorizontalAlignment and VerticalAlignment to something other than Stretch (which is the default). This will make the TextBox shrink to fit its contents. If you then stick it inside a StackPanel with Orientation of Horizontal it will not scroll vertically but be clipped by its containing panel, which I think is what you are after. If you want you containing panel to resize itself to the text, then you need to configure the panel accordingly

将水平对齐和垂直对齐设置为非拉伸(默认)。这将使文本框缩小以适应其内容。如果你把它放在一个水平方向的堆叠板里它不会垂直滚动,而是被它的嵌板夹住,我想这就是你想要的。如果您希望包含面板来调整文本大小,则需要相应地配置面板

#2


0  

        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;

You already have string to set textbox. Firstly, you can calculate like above to measure proper size of richtextbox. you can just set intWidth and intHeight at textbox. I think that makes you helpful.

您已经有了要设置文本框的字符串。首先,你可以像上面那样计算来测量richtextbox的大小。你可以设置文本框的宽度和宽度。我觉得这对你有帮助。

#3


-1  

Try using the ViewBox

尝试使用ViewBox

<Viewbox>
    <RichTextBox />
</Viewbox>