允许调整 TextBox 的大小但不根据用户输入增长 [英] Allow TextBox to be resized but not to grow on user input

查看:12
本文介绍了允许调整 TextBox 的大小但不根据用户输入增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口中定义了一个文本框,如下所示:

I have a TextBox defined inside a window like so:

<Window x:Class="NS.MainWindow"
    ...
    SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition MinWidth="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="50" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0">Description:</TextBlock>

        <TextBox Grid.Column="1" Grid.Row="0" TextWrapping="WrapWithOverflow" />
    </Grid>
</Window>

问题在于,当用户在 TextBox 中键入时,它会向右扩展,因为只设置了 MinWidth.我真正想要的是换行到下一行的文本.如果我将列上的 MinWidth 更改为 Width ,我可以做到这一点.但是,如果我这样做,则在调整窗口大小时 TextBox 不再调整大小.

The problem is that when the user types in the TextBox it expands to the right since only the MinWidth is set. What I really want is the text to wrap to the next line. I can get it to do this if I change the MinWidth on the column to be Width instead. However if I do this, then the TextBox no longer resizes when the Window is resized.

有没有办法让我两者兼得?(即仅在窗口调整大小时调整大小,否则换行)

Is there a way I can have both? (i.e. resize only on Window resize, otherwise wrap)

推荐答案

您出现这种行为的原因是因为您设置了 Window 的 SizeToContent 属性 - 它基本上授权 Window 调整大小本身基于其内容请求的大小.所以当你输入更多的东西时,文本框说我需要更多的空间,窗口就乖乖地变大了.如果您不设置 SizeToContent 属性,您的文本框将不会增长.

The reason you're having this behavior is because you've set the Window's SizeToContent property - which basically authorizes the Window to resize itself based on the size requested by its content. So as you type in more stuff, the textbox says I need more space, the window obediently grows. Your textbox would not grow if you don't set the SizeToContent property.

所以我想说失去 SizeToContent 属性设置器 &使用比例网格大小.这里我说使 Column#2 的宽度是 Column#1 的两倍.Grid 的 Horizo​​ntalAlignment 和 VerticalAlignment 的默认拉伸"值应确保您的控件在调整窗口大小时正确调整大小.

So I'd say lose the SizeToContent property setter & Use proportional grid sizing. Here I say make Column#2 twice the width of Column#1. The default "Stretch" value of HorizontalAlignment and VerticalAlignment for the Grid should ensure that your controls resize correctly on a window resize.

<Window ...
    Title="MyWindow" WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip"
        MinWidth="300" Width="300" Height="80">
    <Grid x:Name="myGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" MinWidth="100"/>
            <ColumnDefinition Width="2*" MinWidth="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="50" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Grid.Row="0">Description:</TextBlock>
        <TextBox Grid.Column="1" Grid.Row="0" TextWrapping="WrapWithOverflow"/>
    </Grid>

如果您只是将 SizeToContent 属性设置器添加回上面的代码片段...您会看到一些奇怪的行为,其中文本框最初随着文本内容增长.. 但是如果您调整窗口大小一次.. 文本框将停止增长.奇怪...无法解释这种行为.
高温

If you just add the SizeToContent property setter back to above code snippet... you'd see some weird behavior where the textbox initially grows with text content.. however if you resize the window once.. the textbox would stop growing. Strange... can't explain that behavior.
HTH

这篇关于允许调整 TextBox 的大小但不根据用户输入增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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