如何在WPF中的CellTemplate中居中TextBlock?

时间:2011-03-16 22:42:56

标签: c# .net wpf listview layout

我同时尝试了HorizontalAlignmentTextAlignment,但它没有效果。

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

有什么想法吗?

编辑:

Xaml代码:

<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>

2 个答案:

答案 0 :(得分:7)

你尝试过Horizo​​ntalContentAlignment吗?在栏目上?

- 编辑 -

原来很难找到放置此Horizo​​ntalContentAlignment的正确位置... :)

使用此

<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</ListView.Resources>
列表视图中的

Snoop通常可以帮助您走上正确的道路。此ListPresenter由ListViewItem使用,它从ListViewItem上的ContentAlignment获取其Alignment。你无法直接访问ListViewItem,这就是你需要Style

的原因

和完整代码:

<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>

答案 1 :(得分:1)

不幸的是,无法在TextBlocks内调整CellTemplate的水平对齐方式。 由于GridViewRowPresenterHorizontalAlignment的{​​{1}}属性设置为其Cells的{​​{1}},在这种情况下为HorizontalContentAlignment

因此,您必须更改TemplatedParent

如果使用snoop更改ListViewItem的{​​{1}},您将看到在创建单元格期间设置ListViewItemStyle值后,单元格的对齐不会受到影响。< / p>