27

Student型のコレクションにバインドされているItemsControlがあります。ItemTemplate内には、IValueConverterを使用してカスタム計算とロジックを実行するTextBoxがあります。そのプロパティではなく、実際の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 に答える