如何在WPF数据表格的第一列显示行号

11
在搜索过程中,我发现可以轻松地将行号设置为行标题:
void datagrid_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
     e.Row.Header = e.Row.GetIndex(); 
} 

它在RowHeader中设置行号。但我想显示行号到第一列。
有人能帮我实现这个吗?谢谢

我尝试使用以下代码: void datagrid_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Header = e.Row.GetIndex(); } 它可以将行号设置在行标题中,但我需要将其设置在列中。 - Hardik
2个回答

19
使用DataGridRow类的GetIndex()方法可能是更简单的方法,这将返回数据源中的索引,但可能不完全符合您的需求。

enter image description here

该XAML

  <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={local:RowToIndexConverter}}" />
            </DataGrid.Columns>
            <DataGrid.Items>
                <sys:String>a</sys:String>
                <sys:String>b</sys:String>
                <sys:String>c</sys:String>
                <sys:String>d</sys:String>
                <sys:String>e</sys:String>
            </DataGrid.Items>
        </DataGrid>
    </Window>

和转换器。

public class RowToIndexConverter : MarkupExtension, IValueConverter
{
    static RowToIndexConverter converter;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridRow row = value as DataGridRow;
        if (row != null)
            return row.GetIndex();
        else
            return -1;
    }

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

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (converter == null) converter = new RowToIndexConverter();
        return converter;
    }

    public RowToIndexConverter()
    {
    }
}

3
谢谢Andy,它奏效了。只需要稍作修改,行号为row.GetIndex() + 1。 - Hardik
不错,但对我来说,在设计时它会多出一行。我的itemssource中有三个项目,但在网格中却得到了四行,最后一行的索引为4,所有列都为空。 - Pompair
6
当有许多行并且启用了行虚拟化时,它无法工作。 - Gildor
3
对我来说这个方法几乎奏效了:当一个DataGrid与ObservableCollection绑定时,索引值在初始状态下是正确的,但如果移除掉一个元素后,被移除元素上面的元素的索引值并没有更新(因此你最终会得到“0, 1, 2, 4, ...”)。 - BCA
1
@Gildor是正确的,在Datagrid上设置EnableRowVirtualization="False" ... 请查看https://dev59.com/v2445IYBdhLWcg3w3N2z#29852087 - alvinchesaro
显示剩余2条评论

4

我使用了安迪的示例,但是我需要从代码后端使用它,所以这是我的代码,以防有人想要做同样的事情,因为谷歌搜索结果很少。

C# 代码:

    DataGrid dataGrid = new DataGrid();

    DataGridTextColumn column0 = new DataGridTextColumn();
    column0.Header = "#";

    Binding bindingColumn0 = new Binding();
    bindingColumn0.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1);
    bindingColumn0.Converter = new RowToIndexConvertor();

    column0.Binding = bindingColumn0;

    dataGrid.Columns.Add(column0);

Andy的转换器,刚刚添加了return row.GetIndex() - 1;以从1开始计数,而不是0。

(此内容涉及IT技术,旨在将计数器从0更改为1)

public class RowToIndexConvertor : MarkupExtension, IValueConverter
{
    static RowToIndexConvertor convertor;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridRow row = value as DataGridRow;

        if (row != null)
        {
            return row.GetIndex() + 1;
        }
        else
        {
            return -1;
        }
    }

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

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (convertor == null)
        {
            convertor = new RowToIndexConvertor();
        }

        return convertor;
    }


    public RowToIndexConvertor()
    {

    }
}

不要忘记使用方式

using System;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Markup;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接