Mediator
Mediator

Reputation: 15378

How as items listbox Horizontal Alignment Center?

A have listbox

<ListBox>
    <ListBox.ItemTemplate>
    <DataTemplate>
        <DockPanel>
        <Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" HorizontalAlignment="Center">
        </Button>
        </DockPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

alt text

Upvotes: 1

Views: 13172

Answers (4)

CodeDigger
CodeDigger

Reputation: 217

<ListBox HorizontalContentAlignment="Center">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding yourstuff}" FontSize="72"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

Upvotes: 8

oli.G
oli.G

Reputation: 1350

The accepted answer is right, however I needed a bit more: not only to center, but also to horizontally stretch the items. Just changing HorizontalAlignment to Stretch didn't work, so, here's how it's done:

<ItemsPanelTemplate>
    <StackPanel HorizontalAlignment="Stretch">
        <StackPanel.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </StackPanel.Resources>
    </StackPanel>
</ItemsPanelTemplate>

Upvotes: 1

Mediator
Mediator

Reputation: 15378

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel HorizontalAlignment="Center"/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

Upvotes: 1

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59101

You may have to center the DockPanel, rather than the data inside it. I've seen this behavior with StackPanel, because the panel shrinks to the contents. The centering ends up working properly, but in the wrong context.

Upvotes: 0

Related Questions