代码之家  ›  专栏  ›  技术社区  ›  Nostromo

水平或垂直对齐不按预期工作

wpf
  •  0
  • Nostromo  · 技术社区  · 7 年前

    每次我认为我开始了解WPF是如何工作的,它在做一些事情(或者不做),我不明白为什么。愿你发光。

    我想在屏幕上放一些数字,数字应该用 Border 和完整的 边界 应该有彩色背景(而不仅仅是 TextBlock 包含数字。)

    所以我这样做了:

    <ListBox ItemsSource="{Binding Numbers}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="3" Columns="4" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="1" BorderBrush="Black">
                    <TextBlock Text="{Binding .}" />
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    但是尽管我宣布 HorizontalAlignment VerticalAlignment 作为 Stretch 边框(以及示例中未包含的背景)仅覆盖 控件 ,而不是 UniformGrid . 我是不是错过了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   FrankM    7 年前

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>