如何在 WPF Datagrid 的第一列中显示行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15061013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show row-number in first column of WPF Datagrid
提问by Hardik
While searching I found that, row number can be set to RowHeader easily:
在搜索时我发现,行号可以轻松设置为 RowHeader:
void datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex();
}
It sets row number in RowHeader. But I want to show Row number to first column
它在 RowHeader 中设置行号。但我想将行号显示到第一列
Can any one help me how can I achieve this? Thanks
任何人都可以帮助我如何实现这一目标?谢谢
回答by Andy
There might be an easier way to do this but I got it to work by using the GetIndex() method on the DataGridRow class. This returns the index in to the data source so might not be exactly what you're after.
可能有一种更简单的方法来做到这一点,但我通过使用 DataGridRow 类上的 GetIndex() 方法让它工作。这会将索引返回到数据源,因此可能不是您所追求的。


the xaml
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>
and the converter.
和转换器。
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()
{
}
}
回答by metoyou
I used Andys example, but I needed to use it from the code behind, so he is my code just in case anyone is looking to do the same as googling was a bit barren.
我使用了 Andys 示例,但我需要从后面的代码中使用它,所以他是我的代码,以防万一有人想做同样的事情,因为谷歌搜索有点贫瘠。
The C#
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's Converter, just added return row.GetIndex() - 1;to start the count at 1, rather than 0.
安迪的转换器,刚刚添加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()
{
}
}
Also don't forget the using's
也不要忘记使用的
using System;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Markup;

