【问题标题】:Listbox using UniformGrid - Items not centered使用 UniformGrid 的列表框 - 项目未居中
【发布时间】:2014-03-03 21:02:08
【问题描述】:

我有一个使用 UniformGrid 作为 ItemsPanelTemplate 的列表框。这是一张照片列表。我希望照片在网格的每个单元格的中心水平居中,但似乎无论我做什么,图像都与每个单元格的左侧对齐。这是我当前的 XAML:

<Border BorderThickness="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Right">
    <ListBox Name="PhotosListBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding Path=photo}" HorizontalAlignment="Center"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Border>

如您所见,我将 DataTemplate 中的 Image 控件设置为 Horizo​​ntalAlignment="Center",我认为可以这样做,但它不起作用。

我做错了什么?

【问题讨论】:

    标签: wpf xaml uniformgrid


    【解决方案1】:

    您需要将HorizontalContentAlignment 设置为Stretch 以首先允许ListBoxItems 拉伸到所有可用空间,以便内联控件可以相应地在中心对齐。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2013-06-04
      • 2011-07-17
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多