【问题标题】:WPF create object using IValueConverter with binding object's propertiesWPF 使用具有绑定对象属性的 IValueConverter 创建对象
【发布时间】:2016-02-12 16:31:22
【问题描述】:

我用来在 XAML 中绑定的对象只能具有字符串属性。但是在绑定中我需要其他类型。我认为我使用 IValueConverter 中的 Converter 函数,我将从字符串属性创建对象并返回它。一个字符串属性将为空,在绑定中我将从 Converter 方法返回其他对象。我试过了,但在 Convert 方法中,我来自 ObservableCollection 的主要对象为空。这是我的 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);

    }

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    我想在 Converter 方法中传递我的实际父对象。解决办法就是改变

    Maps:MapControl.Location="{Binding Location, 
                            Converter={StaticResource StringToGeopoint}}" 
    

    到

    Maps:MapControl.Location="{Binding Converter={StaticResource StringToGeopoint}}" 
    

    它有效:)

    【讨论】:

      【解决方案2】:

      绑定的对象被输入到 Convert()-Method 的“value”参数中。

      您正在访问对应的参数

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

      /更新:

      Maps:MapItemControl 上的 ItemsSource={Binding} 绑定到父对象的 DataContext。这应该是您的 ObservableCollection。

      在 ItemTemplate 中,您的图像有一个“位置”属性,该属性绑定到 ObservableCollection 中每个项目的“位置”属性。你也可以写:

      {Binding Path=Location, Converter={StaticResource StringToGeopoint}}
      

      现在,在完全评估该绑定之前,将存储在 Location-property 中的 Object 传递给转换器,然后将结果传递给 Image 上的“Location”-Property。

      如果您将空对象传递给“值”参数,这意味着原始绑定将空值传递给转换器,因为源对象上的属性为空或属性不存在.

      【讨论】:

      • 我应该在 ConverteParameter 中添加什么?我在 Converter 方法中更改为参数,但它仍然无法正常工作。也许我会再解释一次。在 Converter 方法中,我想访问包含 Location 属性的对象。
      • ConverterParameter 是静态属性,不能通过绑定更改。但是你原来的Binding可能不正确,我会编辑原帖解释一下。
      • 问题是我需要我的对象,它是 Converter 方法中 DataContext 中的 ObservableCollection 中的事件类型。该对象具有字符串经度和纬度。在 Location 中,我需要此字符串中的 Geopoint,我将返回 Binding Path=Location。我的事件类型中的位置是“幽灵财产”。
      猜你喜欢
      • 1970-01-01
      • 2020-04-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多