Haris Hasan
Haris Hasan

Reputation: 30097

How to move text to center of ListBox?

I have almost tried everything but for some reason it is not working

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
    <TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
    <ListBox HorizontalContentAlignment="Stretch" Background="Red" ItemsSource="{Binding Path=ViewOptions}" Margin="10">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

Above XAML give me

enter image description here

How to move the textblock to center of listboxitem?

Upvotes: 3

Views: 2624

Answers (3)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
    <TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
    <ListBox x:Name="listBox" HorizontalContentAlignment="Stretch"  ItemsSource="{Binding Path=ViewOptions}" Margin="10,30,10,10">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                 <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock  HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
   </ListBox>
</StackPanel>

Upvotes: 4

arx
arx

Reputation: 16896

I just tried this and it worked. Two possible differences:

  • My data source was a simple List<string>.
  • I removed the references to your styles (i.e. PhoneTextNormalStyle).

Are you binding to simple data?

Does PhoneTextNormalStyle specify left-alignment?

Upvotes: 1

Raj Ranjhan
Raj Ranjhan

Reputation: 3917

You need to change the HorizontalContentAlignment to center

<StackPanel Orientation="Vertical" Grid.Row="1" Margin="5,30,5,10">
<TextBlock Text="View Options" FontSize="25" Style="{StaticResource PhoneTextNormalStyle}"/>
<ListBox HorizontalContentAlignment="Center" Background="Red" ItemsSource="{Binding Path=ViewOptions}" Margin="10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock HorizontalAlignment="Center" TextAlignment="Center" Text="{Binding}" FontSize="35" Margin="20" Style="{StaticResource PhoneTextNormalStyle}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 1

Related Questions