27

I have ItemsControl that is bound to collection of type Student. Inside the ItemTemplate I have a TextBox that uses IValueConverter to do some custom calculations and logic. I want to pass the actual Student object to the value converter, instead a property of it. How can I do that? Here's a sample of my code.

 <ItemsControl ItemsSource="{Binding StudentList}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}" />
                                    <TextBlock Text="{Binding ????, Converter={StaticResource MyConverter}}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
 </ItemsControl>

In the code I have this

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;
        }
} 
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72

1 Answers1

41

You can just leave out the path. That way you get at the actual object bound to.

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

or if you want to be explicit about it:

<TextBlock Text="{Binding Path=., Converter={StaticResource MyConverter}}"/>
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 4
    Thank you, I'll go and embarrass myself now. – Tomislav Markovski Dec 02 '10 at 12:49
  • 3
    Well it's not immediately obvious. You could mark this as answer if it helped. :) By the way, by using {Binding Path=.} you get the same result. – Botz3000 Dec 02 '10 at 12:59
  • 1
    @TomislavMarkovski I hope you have recovered from your embarrassment. It isn't immediately obvious and it's difficult trying to sift through all the information about binding to properties. – Daniel Hollinrake Sep 22 '16 at 12:11