You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Syncfusion TreeGrid中显示不含堆叠表头的行索引

解决Syncfusion TreeGrid行索引包含堆叠表头行的问题

你当前通过绑定RowIndex获取的索引会包含表头行,原因是RowIndex是控件内部的全局行序号,包含了所有渲染的行(包括堆叠表头行)。可以通过以下两种方式修正:

方式一:使用值转换器过滤表头行

  1. 创建值转换器,判断当前行类型,仅为数据行时计算并显示正确索引:
public class RowIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is TreeGridRowHeaderCell cell)
        {
            // 仅处理数据行
            if (cell.RowData != null && cell.RowType == RowType.DataRow)
            {
                var treeGrid = cell.GetVisualParent<SfTreeGrid>();
                if (treeGrid != null)
                {
                    // 从1开始计数数据行索引
                    return treeGrid.View.Records.IndexOf(cell.RowData) + 1;
                }
            }
            // 表头行返回空字符串
            return string.Empty;
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
  1. 在XAML中声明转换器并修改绑定逻辑:
<Page.Resources>
    <local:RowIndexConverter x:Key="RowIndexConverter"/>
    <Style TargetType="syncfusion:TreeGridRowHeaderCell">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="BorderThickness" Value="0.1,0,0.1,0.1" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="IsTabStop" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="syncfusion:TreeGridRowHeaderCell">
                    <Border x:Name="PART_RowHeaderCellBorder"
                            BorderBrush="Gray"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource RowIndexConverter}}"
                                      TextAlignment="Center"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

方式二:直接绑定数据记录的索引

View.Records集合仅包含数据行,可直接绑定该集合的索引:

<TextBlock HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Text="{Binding RowData, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource RecordIndexConverter}, ConverterParameter={x:Reference treeGrid}}"
          TextAlignment="Center"/>

对应的简化转换器:

public class RecordIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && parameter is SfTreeGrid treeGrid)
        {
            return treeGrid.View.Records.IndexOf(value) + 1;
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

两种方式都会让堆叠表头行的索引区域显示空内容,仅数据行显示连续的正确序号。

内容的提问来源于stack exchange,提问作者gyutae kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:25:16