C# 图像在列表框中奇怪地排列
我制作了一个列表框,其中包含如下所示的ItemTemplate:C# 图像在列表框中奇怪地排列,c#,wpf,xaml,C#,Wpf,Xaml,我制作了一个列表框,其中包含如下所示的ItemTemplate: <ListBox.ItemTemplate> <DataTemplate> <Grid Height="80" Width="80"> <Image Stretch="Uniform" Source="{Binding image.ImageSource}" Horizo
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="80" Width="80">
<Image Stretch="Uniform" Source="{Binding image.ImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<Image HorizontalAlignment="Center" />
我试图用图像填充列表框(正如您可能已经注意到的)
它的工作与绑定刚刚好,但是,图像似乎以一种奇怪的方式排列,即使我已经设置了图像在网格内居中
<Image HorizontalAlignment="Center" />
下面是一个示例图像:
<Image HorizontalAlignment="Center" />
<Image HorizontalAlignment="Center" />
请注意,在图像的右侧有一条小的蓝线,好像我有边距
<Image HorizontalAlignment="Center" />
我有一个固定的高度和宽度的网格,以保持列表框好看
<Image HorizontalAlignment="Center" />
我使用以下XAML实现了项目的水平堆栈:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Image HorizontalAlignment="Center" />
有人知道如何使这些图像在网格中居中吗
<Image HorizontalAlignment="Center" />
谢谢您是否尝试过图像的水平对齐标记
<Image HorizontalAlignment="Center" />
默认的Aero主题为
ListBoxItems
定义了2,0,0,0
的填充,因此内容左侧有空间。要摆脱它,请定义自己的ListBox.ItemContainerStyle
并覆盖它
<Image HorizontalAlignment="Center" />
比如:
<Image HorizontalAlignment="Center" />
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding"
Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
效果很好!非常感谢:)