0

我已经尝试了几乎所有对齐和边距的组合,但没有成功。ListBox 位于 WPF 页面上,该页面绑定到主窗口上的框架。

ListBox 本身很好。它就像我预期的那样对齐。我的 ListBoxItem 包含一个图像和一个文本块。TextBlock 被切断。

从下图中可以看出,事情大多都很好。

在此处输入图像描述

ListBox 与蓝色框的左边缘和顶部有适当的偏移。内容 Image 和 TextBlock 相当居中,ListBoxItem 轮廓也是如此。这是在我的开发机器上。

我认为使用网格、网格线和对齐属性的全部原因是我们不必设置硬编码大小。我们的控件会自行调整大小。这实际上对其他一切都很好。

但是当我把它放在一个小屏幕上时,我得到了这个:

在此处输入图像描述

ListBox 本身仍然正确对齐。但是现在 ListBoxItem 被强制关闭。它的顶部对齐仍然很好,但是底部被推下,所以我们看不到 TextBlock 或 ListBoxItem 的底部。

这是我的 XAML:

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="1">
    <Grid.RowDefinitions>
        <RowDefinition Height="292*" />
        <RowDefinition Height="30*"/>
    </Grid.RowDefinitions>

    <ListBox Grid.Row="1" Name="lbButtons" Margin="2,0,0,1.5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible" >
        <StackPanel Name="spListBoxItems" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Orientation="Horizontal">
            <ListBoxItem Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="10,5,10,5">
                    <Image Source="/COBAN.CobanConsole.UI;component/Images/Buttons/Light/record48_light.png" />
                    <TextBlock Text="Record" Margin="5,5,0,0"></TextBlock>
                </StackPanel>
            </ListBoxItem>
        </StackPanel>
    </ListBox>
</Grid>

没什么。关于发生了什么的任何想法?

4

1 回答 1

0

您在网格行定义中使用 * 单位。这些是相对度量:它们只是告诉您行可以占用的空间比例。如果您需要列表框具有特定高度,则应将第二行更改为具有绝对大小,如下所示:

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="30"/>
</Grid.RowDefinitions>
于 2013-02-08T00:10:32.767 回答