如何显示在WPF数据网格的第一列行号(How to show row-number in first

2019-07-20 11:57发布

当搜寻我发现,行号可以很容易地设置的rowHeader:

void datagrid_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
     e.Row.Header = e.Row.GetIndex(); 
} 

它设置的rowHeader行号。 但是我想表明的行号到第一列

任何一个可以帮助我,我怎么能做到这一点? 谢谢

Answer 1:

有可能做到这一点更简单的方法,但我把它用在DataGridRow类GetIndex()方法来工作。 这将返回该指数的数据来源,从而可能不是你是什么之后。

在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()
    {
    }
}


Answer 2:

我用Andys例子,但我需要从后面的代码中使用它,所以他是我的代码,以防万一有人正在做,因为google搜索有点荒芜一样。

在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);

安迪的转换器,只是增加return row.GetIndex() - 1; 在1开始计数,而不是0。

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;


文章来源: How to show row-number in first column of WPF Datagrid