开发者

WPF pass parent binding object to the converter

开发者 https://www.devze.com 2023-01-28 04:37 出处:网络
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

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 开发者_StackOverflowparameter, System.Globalization.CultureInfo culture)
        {
            // I want 'value' to be of type Student.
            return null;
        }
} 


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}}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号