WPF将父绑定对象传递给转换器

Tom*_*ski 23 wpf binding relativesource parent

我有ItemsControl绑定到Student类型的集合.在ItemTemplate中我有一个TextBox,它使用IValueConverter来做一些自定义计算和逻辑.我想将实际的Student对象传递给值转换器,而不是它的属性.我怎样才能做到这一点?这是我的代码示例.

 <ItemsControl ItemsSource="{Binding StudentList}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}" />
                                    <TextBlock Text="{Binding ????, Converter={StaticResource MyConverter}}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
 </ItemsControl>
Run Code Online (Sandbox Code Playgroud)

在代码我有这个

public class MyValueConverter : IValueConverter
{
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // I want 'value' to be of type Student.
            return null;
        }
} 
Run Code Online (Sandbox Code Playgroud)

Bot*_*000 39

你可以忽略这条路.这样你就可以得到绑定的实际对象.

<TextBlock Text="{Binding Converter={StaticResource MyConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

或者如果你想明确它:

<TextBlock Text="{Binding Path=., Converter={StaticResource MyConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,我现在就去尴尬. (4认同)
  • 嗯,这不是很明显.如果它有帮助,您可以将其标记为答案.:)顺便说一句,通过使用{Binding Path =.},您可以获得相同的结果. (3认同)