WPF:允许文本框大小进行调整,但不会生长在用户输入文本框、生长、大小、用户

2023-09-04 22:31:36 作者:温柔一刀~

我在一个窗口定义,像这样一个文本框:

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列是宽度来代替。但是,如果我这样做,那么文本框不再重新调整时,调整窗口的大小。

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)

推荐答案

你有这种现象的原因是因为你设置了窗口的SizeToContent财产 - 这基本上是授权窗口来调整本身基于由它的内容所要求的大小。所以当你输入更多的东西,文本框说我需要更多的空间,窗口乖乖地增长。你的文本框不会增长,如果你不设置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属性setter和放大器;使用比例的网格大小。在这里,我说的做柱#2的两倍列#1的宽度。默认的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属性setter回到上面的code片段......你会看到一些奇怪的行为,其中的文本与最初文本内容的增长。但是,如果你调整窗口大小..文本框一次将停止增长。奇怪......无法解释的行为。 心连心

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