【问题标题】:How to center text in a specific column in WPF ListView?如何在 WPF ListView 的特定列中居中文本?
【发布时间】:2011-07-03 04:33:35
【问题描述】:

我尝试了这个和 Horizo​​ntalAlignment,而不是 TextAlignment,但它们仍然显示为左对齐。

<Window x:Class="EditorWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="800" Width="600">
    <Grid>
        <ListView ItemsSource="{Binding Effects}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"  />
                    <GridViewColumn Width="100" Header="Type" >
                        <GridViewColumn.CellTemplate >
                            <DataTemplate>
                                <TextBlock Text="{Binding Type}" TextAlignment="Center"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="100" Header="Opacity" DisplayMemberBinding="{Binding Opacity}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

【问题讨论】:

    标签: .net wpf xaml listview styling


    【解决方案1】:

    尝试将 ItemContainerStyle 的 HorizontalContentAlignment 设置为 Stretch。然后它应该与TextAlignment="Center"HorizontalAlignment="Center" 一起使用TextBlock

    <ListView ItemsSource="{Binding Effects}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <!--...-->
    </ListView>
    

    【讨论】:

    • 太棒了。有人能解释一下为什么你必须这样做才能让它工作吗?
    • 如何仅对特定列执行相同操作
    • 我使用 但它不起作用
    • 别忘了从列中删除DisplayMemberBinding 属性,否则CellTemplate 根本不会被应用
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多