WPF将父绑定对象传递到转换器
-
30-09-2019 - |
题
我有itemscontrol,这些extimcontrol与type Student的收集在一起。在ItemTemplate内部,我有一个文本框,该文本框使用ivalueconverter来执行一些自定义计算和逻辑。我想将实际的学生对象传递到值转换器,而将其属性传递给其属性。我怎样才能做到这一点?这是我的代码的示例。
<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;
}
}
解决方案
您可以忽略道路。这样,您就可以按绑定到的实际对象。
<TextBlock Text="{Binding Converter={StaticResource MyConverter}}"/>
或者,如果您想明确说明:
<TextBlock Text="{Binding Path=., Converter={StaticResource MyConverter}}"/>
不隶属于 StackOverflow