Joan Venge
Joan Venge

Reputation: 331200

How to center a TextBlock within a CellTemplate in WPF?

I tried both HorizontalAlignment and TextAlignment but it has no effect.

<GridViewColumn Width="Auto" Header="Type">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Type}" HorizontalAlignment="Center"  TextAlignment="Center" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Any ideas?

EDIT:

Xaml code:

<Window x:Class="EffectsWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Effects Editor"
    <DockPanel VerticalAlignment="Stretch">
        <DockPanel.Resources>

        <ListView
                  DockPanel.Dock="Top"
                  HorizontalContentAlignment="Stretch"
                  ItemsSource="{Binding EditorViewModel.AllEffects}">

            <ListView.View>
                <GridView>
                    <GridViewColumn Width="Auto"
                                    DisplayMemberBinding="{Binding Name}"
                                    Header="Name" />

                    <GridViewColumn Width="Auto" Header="Type">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Type}" HorizontalAlignment="Center"  TextAlignment="Center" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>

        <DockPanel Width="100"
                   Height="100"
                   Margin="0,0,20,20"
                   HorizontalAlignment="Right"
                   VerticalAlignment="Bottom">

            <Label Margin="0,0,0,0"
                   Content="Data"
                   DockPanel.Dock="Top" />

            <ListBox
                     VerticalAlignment="Top"/>

        </DockPanel>

    </DockPanel>
</Window>

Upvotes: 3

Views: 9201

Answers (2)

user2003214
user2003214

Reputation: 46

Unfortunatelly there is no way to adjust the horizontal alignment of TextBlocks within CellTemplate. Because GridViewRowPresenter sets the HorizontalAlignment property of Cells to HorizontalContentAlignment of its TemplatedParent, which in this case is ListViewItem.

So you are going to have to change your ListViewItemStyle.

If you change the HorizontalContentAlignment of ListViewItem using snoop, you will see that the alignment of cell will not be affected since HorizontalContentAlignment value is set during the creating of the cells.

Upvotes: 2

Elad Katz
Elad Katz

Reputation: 7591

did you try HorizontalContentAlignment? on the column?

-- EDIT --

turns out it's kinda hard to find the right place to put this HorizontalContentAlignment... :)

use this

<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</ListView.Resources>

in the listview.

Snoop usually helps u to be on the right path. This ContentPresenter is the used by the ListViewItem, and it gets its Alignment from the ContentAlignment on the ListViewItem. u can't access the ListViewItem directly, and that's why u need that Style

and the full code:

<DockPanel VerticalAlignment="Stretch">

        <ListView 
          DockPanel.Dock="Top"
          HorizontalContentAlignment="Stretch"
          x:Name="MyListView">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>

        </ListView.Resources>
            <ListView.View>
                <GridView >

                    <GridViewColumn Width="Auto"
                            DisplayMemberBinding="{Binding Name}"
                            Header="Name" />

                    <GridViewColumn Width="Auto" Header="Type">

                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Type}" HorizontalAlignment="Center"  TextAlignment="Center" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>

        <DockPanel Width="100"
           Height="100"
           Margin="0,0,20,20"
           HorizontalAlignment="Right"
           VerticalAlignment="Bottom">

            <Label Margin="0,0,0,0"
           Content="Data"
           DockPanel.Dock="Top" />

            <ListBox
             VerticalAlignment="Top"/>

        </DockPanel>

</DockPanel>

Upvotes: 10

Related Questions