WPF通父对象绑定到转换器 [英] WPF pass parent binding object to the converter

查看:338
本文介绍了WPF通父对象绑定到转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到类型学生的集合的ItemsControl。
ItemTemplate模板里面我有一个使用的IValueConverter做一些自定义计算和逻辑一个TextBox。我想通过实际的Student对象的值转换器,而不是它的属性。我该怎么办呢?这里是我的code的样本。

 <的ItemsControl的ItemsSource ={结合StudentList}>
                            < ItemsControl.ItemTemplate>
                                <&DataTemplate的GT;
                                    < TextBlock的文本={绑定名称}/>
                                    < TextBlock的文本={结合????,转换器= {StaticResource的MyConverter}}/>
                                < / DataTemplate中>
                            < /ItemsControl.ItemTemplate>
 < / ItemsControl的>

在code我有这个

 公共类MyValueConverter:的IValueConverter
{
      公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            //我要的'价值'是Student类型的。
            返回null;
        }
}


解决方案

您可以只离开了路径。这样,你得到在绑定到实际的对象。

 < TextBlock的文本={结合转换器= {StaticResource的MyConverter}}/>

如果你想明确一下:

 < TextBlock的文本={绑定路径=,=转换器的StaticResource {} MyConverter}/>

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

解决方案

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

这篇关于WPF通父对象绑定到转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆