Wpf 如何作为项目列表框水平对齐中心?
我有一个列表框Wpf 如何作为项目列表框水平对齐中心?,wpf,Wpf,我有一个列表框 <ListBox> <ListBox.ItemTemplate> <DataTemplate> <DockPanel> <Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" HorizontalAlignment="Center">
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" HorizontalAlignment="Center">
</Button>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您可能需要将DockPanel置于中心,而不是其中的数据。我在StackPanel中看到过这种行为,因为面板收缩到内容。居中最终工作正常,但在错误的上下文中。您可能必须居中DockPanel,而不是其中的数据。我在StackPanel中看到过这种行为,因为面板收缩到内容。定心最终工作正常,但在错误的上下文中。公认的答案是正确的,但我需要更多:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
不仅要居中,还要水平拉伸项目。只是将水平对齐更改为“拉伸”不起作用,因此,下面是如何完成的:
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Stretch">
<StackPanel.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</StackPanel.Resources>
</StackPanel>
</ItemsPanelTemplate>
公认的答案是正确的,但我还需要一点: 不仅要居中,还要水平拉伸项目。只是将水平对齐更改为“拉伸”不起作用,因此,下面是如何完成的:
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Stretch">
<StackPanel.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</StackPanel.Resources>
</StackPanel>
</ItemsPanelTemplate>
dockpanel也不是centered@simply:对不起,这只是猜测:dockpanel也不是centered@simply:抱歉,这只是猜测:ListBox.HorizontalContentAlignment ListBox.HorizontalContentAlignment