DataGrid列中的Column / Row索引 [英] Column/Row index in a DataGrid column
问题描述
我希望以下内容能让我在单元格中的列索引:
< DataGridTemplateColumn Header =Rec。No 。 Width =100IsReadOnly =True>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text ={Binding Source = {RelativeSource AncestorType = DataGridCell},Path = Column.DisplayIndex}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
但它没有。任何人都可以告诉我这里有什么问题?
我实际上正在寻找行索引(需要我的网格中的记录编号栏),但是由于 DataGridRow
似乎没有一个索引类的属性,我试图首先为列索引,它有 DisplayIndex
。但是即使这样也不行。
绑定语法不正确。而不是来源
,它应该是 RelativeSource
:
Text ={Binding RelativeSource = {RelativeSource AncestorType = DataGridCell},
Path = Column.DisplayIndex}
而为了获得 RowIndex
的第二个问题,没有内置的属性例如DataGridRow上的 RowIndex
我建议在底层数据类中绑定一些属性
但是,您也可以手动获取rowIndex,方法是使 IValueConverter 到位。
$ b $公共类RowIndexConverter:IValueConverter{
公共对象转换(对象值,类型targetType,对象参数,
系统)b
全球化文化信息文化)
{
DependencyObject item =(DependencyObject)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
return ic.ItemContainerGenerator.IndexFromContainer(item);
公共对象ConvertBack(对象值,类型targetType,对象参数,
System.Globalization.CultureInfo文化)
{
return Binding.DoNothing ;
}
}
XAML: p>
< TextBlock
Text ={Binding RelativeSource = {RelativeSource AncestorType = DataGridRow},
Converter = StaticResource RowIndexConverter}}/>
当然,您需要在XAML的资源部分声明Converter的实例来使用它。 p>
I was hoping the following would get me column index in the cell:
<DataGridTemplateColumn Header="Rec. No." Width="100" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source={RelativeSource AncestorType=DataGridCell}, Path=Column.DisplayIndex}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
But it didn't. Anyone can tell me what's wrong here?
I'm actually looking for the row index (need a Record No. column in my grid), but since DataGridRow
doesn't apparently have an "index" kind of property, I tried to first do it for column index, which has got DisplayIndex
. But even this one doesn't work.
Binding syntax is incorrect. Instead of Source
, it should be RelativeSource
:
Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell},
Path=Column.DisplayIndex}"
And for second problem for getting RowIndex
, there is no inbuilt property such as RowIndex
on DataGridRow.
I would suggest to have some property in underlying data class and bind to that.
However, you can get rowIndex manually as well by having IValueConverter in place.
public class RowIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
DependencyObject item = (DependencyObject)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
return ic.ItemContainerGenerator.IndexFromContainer(item);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
XAML:
<TextBlock
Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow},
Converter={StaticResource RowIndexConverter}}"/>
Ofcourse, you need to declare instance of Converter under resources section of your XAML to use that.
这篇关于DataGrid列中的Column / Row索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!