27

我有绑定到 Student 类型的集合的 ItemsControl。在 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;
        }
} 
4

1 回答 1

41

您可以省略路径。这样你就可以得到绑定到的实际对象。

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

或者,如果您想明确说明:

<TextBlock Text="{Binding Path=., Converter={StaticResource MyConverter}}"/>
于 2010-12-02T12:44:40.803 回答