简体   繁体   English

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

[英]WPF ListBox with bottom-alignment displays at top within StackPanel

I have the following XAML: 我有以下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>

I would like the Canvas to be on top, and the ListBox to be on the bottom. 我希望“画布”位于顶部,“列表框”位于底部。 Since the default orientation for a StackPanel is vertical, I thought I would get the Canvas and the ListBox, in that order, stacked within the StackPanel. 由于StackPanel的默认方向是垂直的,所以我认为我可以按此顺序将Canvas和ListBox堆叠在StackPanel中。

But instead, I get what is shown below: the ListBox is on top, and the Canvas does not show at all. 但是,我得到的是下面显示的内容:ListBox在顶部,而Canvas根本不显示。 What am I doing wrong? 我究竟做错了什么?

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

Since you haven't set any height or width to the canvas, the height and width for the canvas is set to zero and thats why its not shown in the UI. 由于您尚未为画布设置任何高度或宽度,因此画布的高度和宽度被设置为零,这就是为什么它不在UI中显示的原因。

Try this 尝试这个

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

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

</StackPanel>

If it is not mandatory to use StackPanel then you can achieve that by using Grid's * sizing. 如果不是必须使用StackPanel,则可以使用Grid的*大小来实现。

Here is an example: 这是一个例子:

<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>

Output: 输出:

在此处输入图片说明

Or 要么

<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>

Output: 输出:

在此处输入图片说明

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

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