如何使 WPF StackPanel 适合网格单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20147655/
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 Fit WPF StackPanel to Grid Cell
提问by Connor
I have a StackPanel control in my WPF project, and it is in column 0 row 2 of a Grid. How can I autofit the StackPanel size to the size of that grid cell? Setting the StackPanel width and height to "auto" will just size it to its contents. I could explicitly set its width and height to numerical values, but I was wondering if there was a cleaner, more accurate way. Thank you.
我的 WPF 项目中有一个 StackPanel 控件,它位于网格的第 0 列第 2 行。如何将 StackPanel 大小自动调整为该网格单元格的大小?将 StackPanel 的宽度和高度设置为“自动”只会根据其内容调整大小。我可以明确地将其宽度和高度设置为数值,但我想知道是否有更干净、更准确的方法。谢谢你。
Relevant XAML:
相关 XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="74*"/>
<RowDefinition Height="74*"/>
<RowDefinition Height="421*"/>
</Grid.RowDefinitions>
<Label Content="{StaticResource LoginWindow_Title}" Style="{StaticResource TitleH1}" Grid.ColumnSpan="2"/>
<Label Content="{StaticResource LoginWindow_Subtitle}" Style="{StaticResource TitleH2}" Grid.Row="1" Grid.ColumnSpan="2"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Top">
<StackPanel HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top">
<Label Content="Log in"/>
</StackPanel>
</Border>
</Grid>
回答by Sheridan
The answer is always the same... don't use a StackPanelfor layout purposes. They are primarily used to arrange UI elements that are very unlikely to change size. Even if you resized the StackPanelto the correct size, it would not help because a StackPaneldoes notrearrange or resize it's content items. If you want this functionality, you'll have to use one of the other Panelcontrols like a Gridinstead.
答案总是一样的...不要将 aStackPanel用于布局目的。它们主要用于排列不太可能改变大小的 UI 元素。即使你在调整StackPanel到正确的大小,它不会帮助,因为StackPanel它不会重新排列或调整其内容的项目。如果您需要此功能,则必须改用其他Panel控件之一Grid。
回答by icebat
Your StackPanelis not in Grid, it`s inside Border. So for it to take all available space you can set horizontal and vertical alignment to Stretchboth for it and its parent Border:
你StackPanel不在Grid里面,它在里面Border。因此,为了占用所有可用空间,您可以Stretch为其及其父级设置水平和垂直对齐方式Border:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="74*"/>
<RowDefinition Height="74*"/>
<RowDefinition Height="421*"/>
</Grid.RowDefinitions>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderBrush="Black" BorderThickness="1" Grid.Row="2">
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Label Content="Log in"/>
</StackPanel>
</Border>
</Grid>
Even so, like others mentioned, some other panel almost definetely will be better in this case.
即便如此,就像其他人提到的那样,在这种情况下,其他一些面板几乎肯定会更好。
回答by Nozama
<Grid>
<Border>
<StackPanel VerticalAlignment="Center" ...
<Grid>
<Border>
<StackPanel VerticalAlignment="Center" ...

