The binding syntax is invalid . Instead Sourceit should be RelativeSource:
Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell},
Path=Column.DisplayIndex}"
And for the second problem when receiving, RowIndexthere is no built-in property, such as RowIndexin a DataGridRow.
.
rowIndex , IValueConverter.
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}}"/>
, XAML, .