简体   繁体   English

为什么WPF ComboBox的高度会扩展以填充网格单元,而不是在StackPanel中填充?

[英]Why does a WPF ComboBox height expand to fill a grid cell, but not in a StackPanel?

This is the code that expands to fill the entire Grid cell: 这是扩展为填充整个Grid单元格的代码:

<ComboBox Grid.Column="3" Grid.Row="1" >
  <ComboBoxItem>text1</ComboBoxItem>
  <ComboBoxItem>text2</ComboBoxItem>
  <ComboBoxItem>text3</ComboBoxItem>
  <ComboBoxItem>text4</ComboBoxItem>
</ComboBox>

But when I put this in a StackPanel inside the Grid cell it doesn't expand. 但是,当我将其放在Grid单元内的StackPanel中时,它不会扩展。

<StackPanel  Grid.Column="3" Grid.Row="1" Margin="10">
  <ComboBox>
    <ComboBoxItem>text1</ComboBoxItem>
    <ComboBoxItem>text2</ComboBoxItem>
    <ComboBoxItem>text3</ComboBoxItem>
    <ComboBoxItem>text4</ComboBoxItem>
  </ComboBox>
</StackPanel>

What causes the ComboBox to fill to the Height of the Grid cell in the first code above? 是什么导致ComboBox填充到上面第一个代码中的Grid单元格的高度?

As a follow up, do we generally have to set a specific width for a ComboBox, or is there a better way other than locking the width or height down? 作为后续措施,我们通常是否必须为ComboBox设置特定的宽度,或者除了将宽度或高度锁定为较低以外,还有其他更好的方法吗?

Grid and StackPanel behave differently. Grid和StackPanel的行为不同。

By default, the HorizontalAlignment and VerticalAlignment of the control will be "Stretch". 默认情况下,控件的Horizo​​ntalAlignment和VerticalAlignment将为“ Stretch”。 Grid's behaviour is to, indeed, stretch the control to the entire cell. 网格的行为的确是将控件扩展到整个单元。 However, a StackPanel is designed to just put controls underneath one another (if Orientation is Vertical). 但是,StackPanel设计为仅将控件置于彼此之间(如果Orientation为Vertical)。 So, it ignores the "Stretch" VerticalAlignment in this case. 因此,在这种情况下,它将忽略“拉伸” VerticalAlignment。 It does follow it for the HorizontalAlignment, though. 不过,它确实遵循了Horizo​​ntalAlignment。

So I'm afraid the answer is simply "that's how a StackPanel works". 因此,恐怕答案仅仅是“这就是StackPanel的工作方式”。 If you need stretching, you'll need a different control, like a Grid or UniformGrid. 如果需要拉伸,则需要其他控件,例如Grid或UniformGrid。

For the width, that depends on your design. 对于宽度,这取决于您的设计。 Usually you would use some combination of Margin, MinWidth and MaxWidth (or Height) to get the look you want. 通常,您会结合使用Margin,MinWidth和MaxWidth(或Height)以获得所需的外观。

What causes the ComboBox to fill to the Height of the Grid cell in the first code above? 是什么导致ComboBox填充到上面第一个代码中的Grid单元格的高度?

Different panels have different characteristics. 不同的面板具有不同的特征。

A Grid stretches its child element to fill the available space whereas a StackPanel does not. Grid拉伸其子元素以填充可用空间,而StackPanel则不会。 Please refer to the following question more information about this. 请参考以下问题有关此的更多信息。

Why StackPanel does not stretch its children vertically? 为什么StackPanel不能垂直拉伸其子级?

So in your example the Grid makes the StackPanel itself fill the available space but the StackPanel in turn doesn't make the ComboBox fill its available space. 因此,在您的示例中, Grid使StackPanel本身填充了可用空间,但StackPanel却没有使ComboBox填充其可用空间。

You can confirm this by setting the Background property of the StackPanel to some brush. 您可以通过将StackPanelBackground属性设置为某些画笔来确认这一点。 You will then see that the colour of this brush fills the entire cell of the Grid . 然后,您将看到此笔刷的颜色充满了Grid的整个单元。

Unless I am mistaken, I appears that you have set a singular margin on your StackPanel, by default this sets all 4 margins simultaneously to that value. 除非我没有记错,否则似乎您在StackPanel上设置了一个单边距,默认情况下,这会将所有4个边距同时设置为该值。 This will prevent it from filling the Grid cell 这将防止其填充网格单元

Because StackPanel 's MeasureOverride will limit the panel's rendered size to the minimum needed to render its child controls. 因为StackPanelMeasureOverride会将面板的渲染大小限制为呈现其子控件所需的最小尺寸。

In your case it stops the ComboBox to fill the cell. 在您的情况下,它将停止ComboBox填充单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM