【问题标题】:WPF pass parent binding object to the converterWPF 将父绑定对象传递给转换器
【发布时间】:2010-12-02 12:41:04
【问题描述】:

我有绑定到 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;
        }
} 

【问题讨论】:

    标签: wpf binding parent relativesource


    【解决方案1】:

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

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

    或者如果您想明确说明:

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

    【讨论】:

    • 谢谢你,我这就去丢人现眼。
    • 嗯,这不是很明显。如果有帮助,您可以将此标记为答案。 :) 顺便说一句,通过使用 {Binding Path=.} 你会得到相同的结果。
    • @TomislavMarkovski 我希望你已经从尴尬中恢复过来。这不是很明显,而且很难筛选出有关绑定到属性的所有信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2018-01-09
    • 2022-01-19
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多