我有一个带有DataGridTemplateColumns的DataGrid。在模板列中,我使用了一个工作正常的数据触发器。它从数据网格父级检索项计数。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
...
<!-- this works fine! -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}, Path=Items.Count}" Value="1">
...
</DataTrigger>
</DataTemplate>
是否可以检索放置模板的当前 RowIndex?我认为可以绑定到当前的 DataGridRow。不支持"GetIndex()"的绑定路径,例如:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}}, Path=GetIndex()}" Value="0"> <!-- error: GetIndex() -->
有没有替代方法,从 xaml 绑定到DataGridRow.GetIndex()
?
只能绑定到对象的Properties
,而不能绑定到对象的方法。如果要绑定到方法,则需要使用IValueConverter
-
public class MyConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return (value as DataGridRow).GetIndex();
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
并像这样绑定它——
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}},
Converter={StaticResource MyConverter}}"
Value="0">