绑定语法不正确。代替Source
, 它应该是RelativeSource
:
Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell},
Path=Column.DisplayIndex}"
对于第二个问题RowIndex
,没有内置属性,例如RowIndex
在 DataGridRow 上。
我建议在底层数据类中拥有一些属性并绑定到它。
但是,您也可以通过以下方式手动获取 rowIndex值转换器 http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter%28v=vs.110%29.aspx到位。
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 的资源部分声明 Converter 的实例才能使用它。