Articles in categories

Articles

Center item in WPF ListviewColumn

Here's how to center an item in a WPF Listview Column.

Set HorizontalContentAlignment to Stretch for the ItemContainerStyle for the Listview.
If you want all columns to center their content then set it to Center. When you set it to Stretch you can set the horizontal alignment for each colunm separately.

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment"
                      Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

And in the ListviewColumn:

 <GridViewColumn Header="Edit"
                       Width="100">
     <GridViewColumn.CellTemplate>
         <DataTemplate>
            <Button Content="Edit"
                    HorizontalAlignment="Center" />
          </DataTemplate>
     </GridViewColumn.CellTemplate>
 </GridViewColumn>

Full Listview xaml:

<ListView>
	<ListView.ItemContainerStyle>
		<Style TargetType="ListViewItem">
			<Setter Property="HorizontalContentAlignment"
						Value="Stretch" />
		</Style>
	</ListView.ItemContainerStyle>
	<ListView.View>
		<GridView>
			<GridViewColumn Header="Edit"
						  Width="100">
				<GridViewColumn.CellTemplate>
					<DataTemplate>
						<Button Content="Edit"
							     HorizontalAlignment="Center" />
					</DataTemplate>
				</GridViewColumn.CellTemplate>
			</GridViewColumn>
		</GridView>
	</ListView.View>
</ListView>