ListView AlternationIndex:如何使第一列和第一行的前景透明或从第二行和 1 开始编号

ListView AlternationIndex: how to make foreground transparent for first column and row OR start numbering from second row and from 1

在 WPF 中 ListView 我的第一列如下:

<GridViewColumn x:Name="Id" 
                Header="#"
                DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex),
                RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>

这会自动以增量方式对行进行编号。这看起来像下图:

现在我要做的是让第一行第一列的内容前景透明,我的意思是,我希望图像中显示的 0 不显示,所以我想它的前景透明。我正在尝试对该列应用触发器并检测该值何时为 0,如果是,则使其前景透明以隐藏它,但我不知道该怎么做。

另外我不知道是否可以从 1(而不是 0)和第二行而不是第一行开始对该列进行编号。

有什么想法吗?

您可以使用 TextBlock 创建自定义单元格模板。当交替索引为零时,通过将其 Visibility 设置为 Hidden(或 Collapsed)来添加一种简单地隐藏 TextBlock 的样式。

<GridViewColumn x:Name="Id" 
                Header="#">
   <GridViewColumn.CellTemplate>
      <DataTemplate>
         <TextBlock DataContext="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListViewItem}}"
                    Text="{Binding}">
            <TextBlock.Style>
               <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
                  <Style.Triggers>
                     <DataTrigger Binding="{Binding}" Value="0">
                        <Setter Property="Visibility" Value="Hidden"/>
                     </DataTrigger>
                  </Style.Triggers>
               </Style>
            </TextBlock.Style>
         </TextBlock>
      </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>

交替索引是附加的属性,其中gets its value assigned automatically

The ItemsControl.AlternationIndex is assigned to each item container in the ItemsControl. ItemsControl.AlternationIndex begins at 0, increments until it is AlternationCount minus 1, and then restarts at 0.

改变起始位置对其目的没有意义,它只会改变交替,这就是为什么你不能改变它。您使用它的方式不是它的意思 - 尽管它有效。因此,如果你想有不同的值,你必须在你的数据项中为你的索引公开一个 属性 并相应地设置它们。