gpt4 book ai didi

wpf - 从 xaml 获取 RowIndex

转载 作者:行者123 更新时间:2023-12-04 03:03:14 24 4
gpt4 key购买 nike

我有一个带有 DataGridTemplateColumns 的 DataGrid。在 TemplateColumn 中,我使用了一个工作正常的 DataTrigger。它从 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?我认为可以绑定(bind)到当前的 DataGridRow。不支持“GetIndex()”的绑定(bind)路径,例如:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type DataGridRow}}, Path=GetIndex()}" Value="0"> <!-- error: GetIndex() -->

是否有替代方案,从 xaml 绑定(bind)到 DataGridRow.GetIndex()

最佳答案

您只能绑定(bind)到 Properties 而不是对象的方法。如果您想绑定(bind)到方法,您需要使用 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();
}
}

然后像这样绑定(bind)它-

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type DataGridRow}},
Converter={StaticResource MyConverter}}"
Value="0">

关于wpf - 从 xaml 获取 RowIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com