我在XAML中用于绑定的对象只能包含字符串属性。但在绑定中我需要其他类型。我认为我使用IValueConverter中的Converter函数,我将从字符串属性创建对象并返回此。一个字符串的属性将为空,并且在绑定中我将从Converter方法返回其他对象。我试过这个但是在Convert方法中,我的ObservableCollection主对象是null。这是我的XAML的一部分
<Maps:MapItemsControl ItemsSource="{Binding}">
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Transparent" Tapped="ItemStckPanel">
<Image Source="/Assets/pushpin.gif" Height="30" Width="30"
Maps:MapControl.Location="{Binding Location,
Converter={StaticResource StringToGeopoint}}"
Maps:MapControl.NormalizedAnchorPoint="0.5,0.5"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">
<TextBlock FontSize="20" Foreground="Black" Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
这是我的转换方法:
public object Convert(object value, Type targetType, object parameter, string language)
{
Event _event = (Event) parameter;
BasicGeoposition position = new BasicGeoposition();
position.Latitude = _event.Latitude;
position.Longitude = _event.Longitude;
return new Geopoint(position);
}
答案 0 :(得分:1)
我想在Converter方法中传递我的实际父对象。解决方案是改变
Maps:MapControl.Location="{Binding Location,
Converter={StaticResource StringToGeopoint}}"
到
Maps:MapControl.Location="{Binding Converter={StaticResource StringToGeopoint}}"
有效:)
答案 1 :(得分:0)
绑定对象被输入&#34;值&#34; Convert() - Method的参数。
您正在访问与
对应的参数<... ConverterParameter= .../>
未在您的xaml中设置。
你实际上必须编写你的Convert() - 方法如下:
public object Convert(object value, Type targetType, object parameter, string language)
{
Event _event = (Event) value;
BasicGeoposition position = new BasicGeoposition();
position.Latitude = _event.Latitude;
position.Longitude = _event.Longitude;
return new Geopoint(position);
}
/ UPDATE:
Maps上的ItemsSource = {Binding}:MapItemControl绑定到父对象的DataContext。这应该是你的ObservableCollection。
在ItemTemplate中,您的图像有一个&#34;位置&#34; -Property,它绑定到ObservableCollection中每个项目的&#34; Location&#34; -property。你也可以写:
{Binding Path=Location, Converter={StaticResource StringToGeopoint}}
在完全评估该绑定之前,存储在Location-property中的Object将传递给转换器,然后将结果传递给Image上的&#34; Location&#34; -Property。
如果要将空对象传递给&#34;值&#34; -parameter,则意味着原始Binding将null值传递给Converter,因为源对象上的Property为null或者因为财产不存在。