2014-10-20 111 views
0

可能很简单的东西,但无法弄清楚,为什么我得到这个错误..对象不能转换为类型MyCustomType

Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection[IDATT.Infrastructure.Controls.Map.IMapLayerItem]'.

我工作的自定义控件并尝试设置可绑定属性。错误是下面XAML行: enter image description here

代码段从IdattMap

public static readonly DependencyProperty LayersProperty = 
      DependencyProperty.Register("Layers", typeof(List<IdattMapLayer>), typeof(IdattMap), new PropertyMetadata(null)); 

     public List<IdattMapLayer> Layers 
     { 
      get { return GetValue(LayersProperty) as List<IdattMapLayer>; } 
      set { this.SetValue(LayersProperty, value); } 
     } 

代码从IdattMapLayer片段:

public class IdattMapLayer 
    { 
     public ControlTemplate ControlTemplate { get; set; } 

     public ObservableCollection<IMapLayerItem> MapItemsDataSource { get; set; } 

     public static readonly DependencyProperty MapItemsDataSourceProperty = 
      DependencyProperty.Register("MapItemsDataSource", typeof(ObservableCollection<IMapLayerItem>), typeof(IdattMap), new PropertyMetadata(OnMapItemsDataSourceChanged)); 


     private static void OnMapItemsDataSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
     { 
      var control = sender as IdattMap; 
      if (control == null || control.MapControl == null) return; 
     } 
    } 

。而MappedData势必VM,它是ObservableCollection<IMapLayerItem>类型。

为什么期望“绑定”对象和传递可观察colllection不起作用?

+0

你能不能也请显示'MappedData'资源的声明? – Clemens 2014-10-20 20:34:04

+0

请参阅[this](http://stackoverflow.com/a/9128855/1136211)回答,以解释为什么使用更基本的集合类型可能更好。 – Clemens 2014-10-20 20:37:23

+0

我修正了它..我做了什么,我让IdattMapLayer从DependencyObject继承,它现在即使与OmegaMan的接口 – katit 2014-10-20 20:38:58

回答

0

由于使用反射在幕后执行绑定,需要一定的层次结构来完成所有工作,因此绑定对于接口不太合适。

将您的控件MapItemsDataSource依赖项属性更改为遵守有问题的接口的实际实例的可观察集合。

目前还不清楚这是否针对最新版本的Silverlight进行了更改,但有人在S4上为此线程的接口写了一个动态绑定的包装器。

Binding to an interface where the implementation class is internal. (Silverlight 3)

+0

一起工作,问题是 - 我无法将MapItemsDataSource更改为硬类的集合,需要这个给用户一些自由。我尝试而不是界面,但得到了相同的错误 – katit 2014-10-20 18:50:17

+0

@katit你可以创建一个包装类,它可以容纳对象和公开属性,只是通过? – OmegaMan 2014-10-20 19:22:28

+0

我修正了它..我做了什么,我让IdattMapLayer继承自DependencyObject,它现在即使在接口 – katit 2014-10-20 20:39:22

相关问题