How Do I Center Something In My ListView Column?

So the items in your ListView column look like this:

Centered Example 1

And you want them to look like this:

Centered Example 2

If you’re trying to center something within a ListView column, it’s actually pretty simple.

First, go to your ListView and, if you haven’t already, create an ItemContainerStyle by going to the Object -> Edit Other Styles… -> Edit ItemContainerStyle -> Create Empty…

ItemContainerStyle Menu

Within your ItemContainerStyle, set the HorizontalContentAlignment from “Left” to “Stretch“.

Your ItemContainerStyle XAML should now look like this (important stuff in bold):

<Style x:Key=”MyItemContainerStyle TargetType=”{x:Type ListViewItem}>
      <Setter Property=”HorizontalContentAlignment Value=”Stretch/>
</Style>

A point of note: if you want everything to be centered, you can set it to “Center“. However, if you prefer more control over the layout of your columns (as I do), set it to stretch.

Now, go to the DataTemplate for the column you want to center. (If you don’t know how to get to the DataTemplate or how to create one, you can find that information here. Just look for “DataTemplate”.)

Within the DataTemplate, align your bound item so that the HorizontalAlignment is set to “Center“.

Your DataTemplate should now look something like this (important stuff in bold):

<DataTemplate x:Key=”AuthorTemplate>
      <TextBlock Text=”{Binding Mode=OneWay, XPath=author}FontSize=”12”  HorizontalAlignment=”Center />
</DataTemplate>

Finally, your ListView should already be set up, but just to double check, make sure that your ListView XAML looks something like this:

<ListView ItemContainerStyle=”{DynamicResource MyItemContainerStyle} >
      <ListView.View>
            <GridView>
                  <GridViewColumn CellTemplate=”{DynamicResource AuthorTemplate} Header=”Author/>
            </GridView>
      </ListView.View>
</ListView>

4 thoughts on “How Do I Center Something In My ListView Column?

  1. There are a lot of services out there that claim to bbe the best
    VPN service. The best vpn service also allows you to send your nformation and browse the internet iin complete
    security. However, most libraries will bee unable to sttore and shaqre
    the amount and diversity of information that the Internet can.

  2. selecting items from a particular column in a listview

    bold the items of a particular column in a listview

Comments are closed.