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

时间:2010-12-02 12:41:05

标签: wpf binding parent relativesource

我有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>

在代码中我有这个

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;
        }
} 

1 个答案:

答案 0 :(得分:38)

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

<TextBlock Text="{Binding Converter={StaticResource MyConverter}}"/>

或者如果你想明确它:

<TextBlock Text="{Binding Path=., Converter={StaticResource MyConverter}}"/>
相关问题