10

検索中に、行番号をRowHeaderに簡単に設定できることがわかりました。

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

RowHeaderに行番号を設定します。ただし、最初の列に行番号を表示したい

誰かが私を助けることができますか?これをどのように達成できますか?ありがとう

4

2 に答える 2

18

これを行う簡単な方法があるかもしれませんが、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()
    {
    }
}
于 2013-02-25T07:01:20.783 に答える
4

私は Andys の例を使用しましたが、コード ビハインドからそれを使用する必要があったため、グーグルが少し不毛だったので、誰かが同じことをしようとしている場合に備えて、彼は私のコードです。

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;。カウントを 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;
于 2016-02-18T10:05:23.680 に答える