WPF XAML 和 MinWidth 和 MaxWidth
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30580872/
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
WPF XAML and MinWidth and MaxWidth
提问by ClosDesign
I have StackPanel in which I wrapped 7 Rectangles all varying widths.
我有 StackPanel,我在其中包裹了 7 个不同宽度的矩形。
My First rectangle in the Stack, I have a Min and Max width defined and one of the other Rectangles (see below x:Name="ToBeCollapsed") that is Collapsed by default, BUT is made visible in a certain condition in the C#.
My issue is that the first Rectangle does not stretch to the MAX width if the rectangle "ToBeCollasped" is collapsed. My thought was that the first rectangle would fill the space to the MAX of "755" if the collapsed rectangle was collapsed.
我在堆栈中的第一个矩形,我定义了最小和最大宽度,以及默认情况下折叠的其他矩形之一(见下文 x:Name="ToBeCollapsed"),但在 C# 中的特定条件下可见。
我的问题是,如果矩形“ToBeCollasped”折叠起来,第一个矩形不会拉伸到最大宽度。我的想法是,如果折叠的矩形被折叠,第一个矩形将填充“755”的最大空间。
Am I wrong in my logic?
我的逻辑错了吗?
My layout is below:
我的布局如下:
<StackPanel x:Name="RectangleColumns" Width="1840" Orientation="Horizontal">
<Rectangle MinWidth="575" MaxWidth="755" />
<Rectangle Width="315"/>
<Rectangle Width="180" />
<Rectangle Width="180"/>
<!--If collapsed first rectangle should grow to 755. MinWidth + 180-->
<Rectangle x:Name="ToBeCollapsed" Width="180"/>
<Rectangle Width="220"/>
<Rectangle Width="190"/>
</StackPanel>
采纳答案by James Harcourt
A grid with column definitions set properly will do the trick. I've tried this myself using some fill colours and smaller widths to demonstrate the point, that first column grows into the space when the fifth column collapses:
具有正确设置的列定义的网格可以解决问题。我自己尝试过使用一些填充颜色和较小的宽度来证明这一点,当第五列折叠时,第一列会增长到空间中:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="RectangleColumns" HorizontalAlignment="Stretch ">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200" MaxWidth="400"/>
<ColumnDefinition MaxWidth="50"/>
<ColumnDefinition MaxWidth="50"/>
<ColumnDefinition MaxWidth="50"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MaxWidth="50"/>
<ColumnDefinition MaxWidth="50"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Fill="Aqua" MinWidth="200" MaxWidth="400" />
<Rectangle Grid.Column="1" Fill="Blue" Width="50"/>
<Rectangle Grid.Column="2" Fill="Aqua" Width="50" />
<Rectangle Grid.Column="3" Fill="Blue" Width="50"/>
<Rectangle Grid.Column="4" Fill="Pink" Width="300" Name="toBeCollapsed"/>
<Rectangle Grid.Column="5" Fill="Aqua" Width="50"/>
<Rectangle Grid.Column="6" Fill="Blue" Width="50"/>
</Grid>
<Button Grid.Row="1" Content="Toggle Visibility" Name="ButtonToggle" />
</Grid>
Code-behind, just to demonstrate:
代码隐藏,只是为了演示:
public MainWindow()
{
InitializeComponent();
ButtonToggle.Click += ButtonToggleOnClick;
}
private void ButtonToggleOnClick(object sender, RoutedEventArgs routedEventArgs)
{
toBeCollapsed.Visibility = toBeCollapsed.Visibility == Visibility.Collapsed
? Visibility.Visible
: Visibility.Collapsed;
}
回答by sac1
Always look behind the schene:
总是看幕后:
In WPF, every LayoutControl is a Grid- or reproducable with a Grid - but it uses different Column or Row definitions and Alignment.
在 WPF 中,每个 LayoutControl 都是一个Grid- 或可使用网格重现 - 但它使用不同的列或行定义和Alignment.
Stackpanelis a Grid which contains following setup (Horizontal):
Stackpanel是一个包含以下设置(水平)的网格:
- Columndefinition for every added controls with
Autosize - All added Child controls HorizontalAligment is Center
- Additional Columndefinition with * Width
- 每个添加的控件的列定义
Auto大小 - 所有添加的子控件 HorizontalAligment 为中心
- 带有 * 宽度的附加列定义
And that's the way the cookies cramble.
这就是饼干的方式。
Auto sizing distributes space based on the size of the content that is within a column or row.
自动调整大小根据列或行内的内容大小分配空间。
Which is usually the smallest possible size :/
这通常是可能的最小尺寸:/
So +1 for @Bradley Uffner
所以@Bradley Uffner +1
Use a Gridwhere Columndefinitionof Min/Max Rectangleis *.
使用Grid其中Columndefinition的 Min/MaxRectangle是*。

