Why doesn't the StackPanel stretch its kids vertically?

(new to WPF) I am looking at a WPF example:

<Window x:Class="Attempt_XAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel>
        <Label HorizontalAlignment="Center">A Button Stack</Label>
        <Button HorizontalAlignment="Left">Button 1</Button>
        <Button HorizontalAlignment="Right">Button 2</Button>
        <Button Background="#FFA29494">Button 3</Button>
        <Button>Button 4</Button>
    </StackPanel>
</Window>

A remark in MS notes that:

The default value is stretching for both the HorizontalAlignment parameter and the Vertical Alignment of the contents contained in the StackPanel.

However, the result looks different than I expect. Buttonand Labelthey are not elongated vertically, but only horizontally (i.e. they do not fill the entire space Windowin both directions). Why?

+4
source share
2 answers

The button and label are not elongated vertically, but only horizontally (i.e. they do not fill the entire window in both directions). Why?

, . , StackPanel, - :

  • "measure",
  • "",

StackPanel , - , Horizontal , Vertical. , ( ), . , , VerticalAlignment Stretch, .

, , , (t26) ( Grid - , ). , .


Edit

" ": , StackPanel , . , Button, Label .., ? , , , , VerticalAlignment "Stretch", ? . , , x () - , .. ( VerticalAlignment "Stretch" ).

, :

public class TestControl : ContentControl
{
    public string Description { get; set; }

    protected override Size MeasureOverride(Size availableSize)
    {
        System.Diagnostics.Debug.WriteLine("Size available for '" + Description + "': " + availableSize.Height);
        return base.MeasureOverride(availableSize);
    }
}

, , . Grid a StackPanel :

<Grid Height="50">
    <Grid.RowDefinition />
    <Grid.RowDefinition />

    <local:TestControl Description="in Grid" />

    <StackPanel Grid.Row="1" Height="10">
        <local:TestControl Description="in StackPanel" />
    </StackPanel>
</Grid>

, Grid CustomPanel ( ) 25 ( ). StackPanel, , CustomPanel .

+8

HorizontalAlignment, VerticalAlignment of Content, StackPanel. Orientation.

Orientation Vertical, .

+1

Source: https://habr.com/ru/post/1524619/


All Articles