2014-10-20 164 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

相關問題