我希望能够将列中的文本和一列的单元格居中。 这是我到目前为止拥有的 XAML。 我不知道如何为一个项目而不是所有项目做到这一点。
<ListView
Grid.Column="0"
Margin="3,3,0,3"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Visible"
SelectionMode="Single">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding CopiedFrom}">
<GridViewColumn.Header>
<GridViewColumnHeader>Copied From:</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Name}">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</GridViewColumn.HeaderContainerStyle>
<GridViewColumn.Header>
<GridViewColumnHeader>Date/Time:</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
对XMAL来说相当新。 任何帮助将不胜感激。
将ListViewItem
容器的HorizontalContentAlignment
属性设置为Stretch
,并对要居中的单元格使用带有TextBlock
的CellTemplate
:
<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>