wpf 如何在窗口调整大小时调整文本框的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4495947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make textbox resize as the window resize
提问by user496949
Any property to set to make textbox to resize according to the window size?
设置任何属性以使文本框根据窗口大小调整大小?
回答by Josh
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 更易于使用。
回答by Programista
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!
要使文本框填充整个窗口并使用它调整大小,您需要明确地将其高度设置为自动,即使它是默认值!
回答by Rohit Sachan
It could be little tricky because of few constraints.
由于限制很少,这可能有点棘手。
- 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).
- I can't keep it default because in case of no text it's very small in width, So I needed MinWidth.
- At the same time you have to have a static width representation, because we want to wrap the text for particular line.
- 你不能把宽度设为自动的文本框(在我的情况下,如果某个复制粘贴长字符串,它会因为自动宽度而超出边界)。
- 我不能保持默认,因为在没有文本的情况下,它的宽度非常小,所以我需要 MinWidth。
- 同时你必须有一个静态的宽度表示,因为我们想要为特定行包装文本。
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}"
宽度=“{绑定元素名称=左列定义,路径=宽度}”
to <ColumnDefinition Width="*" Name="LeftColumnDefinition" />
到 <ColumnDefinition Width="*" Name="LeftColumnDefinition" />
回答by Bobby D
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} />
<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 exactlythe Height/Width of the parent window. A lot depends on your specific usage scenario, but this should be enough to get you started.
可能需要绑定到不同的属性,或者您可能想要使用表达式,以便 TextBox 不完全是父窗口的高度/宽度。在很大程度上取决于您的特定使用场景,但这应该足以让您入门。