具有底部对齐的WPF列表框显示在StackPanel的顶部

具有底部对齐的WPF列表框显示在StackPanel的顶部,wpf,listbox,stackpanel,Wpf,Listbox,Stackpanel,我有以下XAML: <Window x:Class="test_stacking.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">

我有以下XAML:

<Window x:Class="test_stacking.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel Background="AliceBlue">

        <Canvas Background="Red">
        </Canvas>

        <ListBox Width="200" VerticalAlignment="Bottom">
            <TextBlock Text="One" />
            <TextBlock Text="Two" />
        </ListBox>

    </StackPanel>

</Window>

我希望画布在顶部,列表框在底部。因为StackPanel的默认方向是垂直的,所以我认为画布和列表框应该按照这个顺序堆叠在StackPanel中

但是,我得到的是如下所示:列表框在顶部,画布根本不显示。我做错了什么


.NET FW 4客户端配置文件,Windows 7,VS 2010。

由于您尚未设置画布的任何高度或宽度,因此画布的高度和宽度设置为零,因此未在UI中显示

试试这个

<StackPanel Background="AliceBlue">
      <Canvas Background="Red" Width="200" Height="200">
      </Canvas>

       <ListBox Width="200" VerticalAlignment="Bottom">
             <TextBlock Text="One" />
             <TextBlock Text="Two" />
       </ListBox>

</StackPanel>

如果不强制使用StackPanel,则可以通过使用Grid的*大小调整来实现

以下是一个例子:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="500" Width="500">
    <Grid Background="AliceBlue">

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Canvas Background="Red">
        </Canvas>

        <ListBox Grid.Row="1" Width="200" VerticalAlignment="Bottom">
            <TextBlock Text="One" />
            <TextBlock Text="Two" />
        </ListBox>

    </Grid>
</Window>

输出:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="500" Width="500">
    <Grid Background="AliceBlue">

        <Canvas Background="Red">
        </Canvas>

        <ListBox Width="200" VerticalAlignment="Bottom">
            <TextBlock Text="One" />
            <TextBlock Text="Two" />
        </ListBox>

    </Grid>
</Window>

输出:


+1是的,这种方法很有效,谢谢。画布是否有办法占用列表框中剩余的所有垂直空间?+1是的,第一种方法正是我想要的!;-)这非常有用,谢谢。