如何在窗口调整大小时调整文本框的大小

时间:2021-07-06 18:59:59

Any property to set to make textbox to resize according to the window size?

是否设置了根据窗口大小调整文本框大小的属性?

4 个解决方案

#1


19  

Layout in WPF is heavily influenced by the parent container. For example, if you are creating a form with labels and input fields, consider using a Grid panel. Controls in WPF by default resize according to the layout behavior of their parent. Here is an example of a window with two labeled text boxes and two buttons that resize along with the window.

WPF中的布局受父容器的影响很大。例如,如果要创建包含标签和输入字段的表单,请考虑使用“网格”面板。默认情况下,WPF中的控件根据其父级的布局行为进行调整。下面是一个窗口示例,其中包含两个带标签的文本框和两个随窗口一起调整大小的按钮。

<Window>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="Contact Name" Grid.Row="0" Grid.Column="0" />
        <TextBox Grid.Row="0" Grid.Column="1" />

        <Label Content="Contact Location" Grid.Row="1" Grid.Column="0" />
        <TextBox Grid.Row="1" Grid.Column="1" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1">
            <Button Content="OK" Width="75" Height="24" Margin="3" />
            <Button Content="Cancel" Width="75" Height="24" Margin="3" />
        </StackPanel>

    </Grid>
</Window>

Or if you wanted something similar to the address bar layout of a browser, you could do something like:

或者,如果您想要类似于浏览器的地址栏布局,您可以执行以下操作:

<Window>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Button Content="Back" DockPanel.Dock="Left" />
            <Button Content="Forward" DockPanel.Dock="Left" />
            <Button Content="Refresh" DockPanel.Dock="Right" />
            <TextBox /> <!-- fill is assumed for last child -->
        <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" />
        <WebBrowser /> <!-- fill is assumed for last child -->
    </DockPanel>
</Window>

Note that in the above example, I nested two DockPanel's. It could also have been achieved with a Grid but the markup would have been much more cluttered. If you are new to WPF, I'd highly suggest playing around with the various panels available to you. Once you learn when to apply a particular panel to a particular layout, it makes WPF much easier to work with.

请注意,在上面的示例中,我嵌套了两个DockPanel。它也可以通过Grid实现,但标记会更加混乱。如果您是WPF的新手,我强烈建议您使用各种面板。一旦您了解何时将特定面板应用于特定布局,就可以更轻松地使用WPF。

#2


3  

Bit late, but i want this know by everybody.

有点晚了,但我希望每个人都知道这一点。

To make textbox fill whole window and resize with it you need to explicitly set its Height to Auto even if it is the default one!

要使文本框填满整个窗口并使用它调整大小,您需要将其高度显式设置为自动,即使它是默认值!

#3


1  

It could be little tricky because of few constraints.

由于几乎没有限制,这可能有点棘手。

  1. Text box you cant put width auto (in my case if some one copy paste long string it goes out of boundary because of Auto Width).
  2. 文本框你不能把宽度自动(在我的情况下,如果一些复制粘贴长字符串,它因为自动宽度而超出边界)。
  3. I can't keep it default because in case of no text it's very small in width, So I needed MinWidth.
  4. 我不能保持默认值,因为如果没有文本,它的宽度非常小,所以我需要MinWidth。
  5. At the same time you have to have a static width representation, because we want to wrap the text for particular line.
  6. 同时你必须有一个静态宽度表示,因为我们想要包装特定行的文本。

I tried this solution which makes your width bounded to parent ( I agree there could be better solutions as well, but this was easy and worked fine)

我试过这个解决方案让你的宽度与父母一致(我同意也可以有更好的解决方案,但这很容易并且工作正常)

<Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" Name="LeftColumnDefinition" />
      <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Column="0" BorderThickness="3" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" ClipToBounds="True"
                         TextWrapping="Wrap" Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"  MinWidth="560" MinHeight="57" AcceptsTab="True" Foreground="{StaticResource darkBlueBrush}">FIX message... </TextBox>
     <Button Margin="2,0,0,0"  Grid.Column="1" Content="Publish FIX Message" Name="PublishFIX" Click="publishFix_Click" HorizontalAlignment ="Center" Height="25"/>

</Grid>

So only good thing I did was, I binned the

所以我唯一的好处就是,我把它装箱了

Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"

Width =“{Binding ElementName = LeftColumnDefinition,Path = Width}”

to <ColumnDefinition Width="*" Name="LeftColumnDefinition" />

to

#4


0  

One method would be to bind the height and width of the TextBox to the height and width of the Window, for example:

一种方法是将TextBox的高度和宽度绑定到Window的高度和宽度,例如:

<TextBox Height={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Height} />

There may be a different property that might want to bind to, or you might want to use an expression so the TextBox isn't exactly the Height/Width of the parent window. A lot depends on your specific usage scenario, but this should be enough to get you started.

可能存在可能要绑定的不同属性,或者您可能希望使用表达式,因此TextBox不完全是父窗口的高度/宽度。很大程度上取决于您的具体使用场景,但这应该足以让您入门。

#1


19  

Layout in WPF is heavily influenced by the parent container. For example, if you are creating a form with labels and input fields, consider using a Grid panel. Controls in WPF by default resize according to the layout behavior of their parent. Here is an example of a window with two labeled text boxes and two buttons that resize along with the window.

WPF中的布局受父容器的影响很大。例如,如果要创建包含标签和输入字段的表单,请考虑使用“网格”面板。默认情况下,WPF中的控件根据其父级的布局行为进行调整。下面是一个窗口示例,其中包含两个带标签的文本框和两个随窗口一起调整大小的按钮。

<Window>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="Contact Name" Grid.Row="0" Grid.Column="0" />
        <TextBox Grid.Row="0" Grid.Column="1" />

        <Label Content="Contact Location" Grid.Row="1" Grid.Column="0" />
        <TextBox Grid.Row="1" Grid.Column="1" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
                    VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1">
            <Button Content="OK" Width="75" Height="24" Margin="3" />
            <Button Content="Cancel" Width="75" Height="24" Margin="3" />
        </StackPanel>

    </Grid>
</Window>

Or if you wanted something similar to the address bar layout of a browser, you could do something like:

或者,如果您想要类似于浏览器的地址栏布局,您可以执行以下操作:

<Window>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Button Content="Back" DockPanel.Dock="Left" />
            <Button Content="Forward" DockPanel.Dock="Left" />
            <Button Content="Refresh" DockPanel.Dock="Right" />
            <TextBox /> <!-- fill is assumed for last child -->
        <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" />
        <WebBrowser /> <!-- fill is assumed for last child -->
    </DockPanel>
</Window>

Note that in the above example, I nested two DockPanel's. It could also have been achieved with a Grid but the markup would have been much more cluttered. If you are new to WPF, I'd highly suggest playing around with the various panels available to you. Once you learn when to apply a particular panel to a particular layout, it makes WPF much easier to work with.

请注意,在上面的示例中,我嵌套了两个DockPanel。它也可以通过Grid实现,但标记会更加混乱。如果您是WPF的新手,我强烈建议您使用各种面板。一旦您了解何时将特定面板应用于特定布局,就可以更轻松地使用WPF。

#2


3  

Bit late, but i want this know by everybody.

有点晚了,但我希望每个人都知道这一点。

To make textbox fill whole window and resize with it you need to explicitly set its Height to Auto even if it is the default one!

要使文本框填满整个窗口并使用它调整大小,您需要将其高度显式设置为自动,即使它是默认值!

#3


1  

It could be little tricky because of few constraints.

由于几乎没有限制,这可能有点棘手。

  1. Text box you cant put width auto (in my case if some one copy paste long string it goes out of boundary because of Auto Width).
  2. 文本框你不能把宽度自动(在我的情况下,如果一些复制粘贴长字符串,它因为自动宽度而超出边界)。
  3. I can't keep it default because in case of no text it's very small in width, So I needed MinWidth.
  4. 我不能保持默认值,因为如果没有文本,它的宽度非常小,所以我需要MinWidth。
  5. At the same time you have to have a static width representation, because we want to wrap the text for particular line.
  6. 同时你必须有一个静态宽度表示,因为我们想要包装特定行的文本。

I tried this solution which makes your width bounded to parent ( I agree there could be better solutions as well, but this was easy and worked fine)

我试过这个解决方案让你的宽度与父母一致(我同意也可以有更好的解决方案,但这很容易并且工作正常)

<Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" Name="LeftColumnDefinition" />
      <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Column="0" BorderThickness="3" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" ClipToBounds="True"
                         TextWrapping="Wrap" Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"  MinWidth="560" MinHeight="57" AcceptsTab="True" Foreground="{StaticResource darkBlueBrush}">FIX message... </TextBox>
     <Button Margin="2,0,0,0"  Grid.Column="1" Content="Publish FIX Message" Name="PublishFIX" Click="publishFix_Click" HorizontalAlignment ="Center" Height="25"/>

</Grid>

So only good thing I did was, I binned the

所以我唯一的好处就是,我把它装箱了

Width= "{Binding ElementName=LeftColumnDefinition, Path=Width}"

Width =“{Binding ElementName = LeftColumnDefinition,Path = Width}”

to <ColumnDefinition Width="*" Name="LeftColumnDefinition" />

to

#4


0  

One method would be to bind the height and width of the TextBox to the height and width of the Window, for example:

一种方法是将TextBox的高度和宽度绑定到Window的高度和宽度,例如:

<TextBox Height={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Height} />

There may be a different property that might want to bind to, or you might want to use an expression so the TextBox isn't exactly the Height/Width of the parent window. A lot depends on your specific usage scenario, but this should be enough to get you started.

可能存在可能要绑定的不同属性,或者您可能希望使用表达式,因此TextBox不完全是父窗口的高度/宽度。很大程度上取决于您的具体使用场景,但这应该足以让您入门。