Center text in XAML Column and ListViewItem

后端 未结 1 2000
时光说笑
时光说笑 2021-01-24 03:19

I want to be able to center the text in the column and the cell for one column. This is the XAML that I have so far. I cannot figure out how to do it for one item opposed to a

相关标签:
1条回答
  • 2021-01-24 03:38

    Set the HorizontalContentAlignment property of the ListViewItem container to Stretch and use a CellTemplate with a TextBlock for the cell you want to center:

    <ListView
            Grid.Column="0"
            Margin="3,3,0,3"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            ScrollViewer.HorizontalScrollBarVisibility="Auto"
            ScrollViewer.VerticalScrollBarVisibility="Visible"
            SelectionMode="Single">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridView.Columns>
                    <GridViewColumn Width="100" DisplayMemberBinding="{Binding CopiedFrom}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader>Copied From:</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
    
                    <!-- Centered: -->
                    <GridViewColumn Width="100">
                        <GridViewColumn.HeaderContainerStyle>
                            <Style TargetType="{x:Type GridViewColumnHeader}">
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                            </Style>
                        </GridViewColumn.HeaderContainerStyle>
                        <GridViewColumn.Header>
                            <GridViewColumnHeader>Date/Time:</GridViewColumnHeader>
                        </GridViewColumn.Header>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
    
    0 讨论(0)
提交回复
热议问题