可能很簡單的東西,但無法弄清楚,爲什麼我得到這個錯誤..對象不能轉換爲類型MyCustomType
Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection[IDATT.Infrastructure.Controls.Map.IMapLayerItem]'.
我工作的自定義控件並嘗試設置可綁定屬性。錯誤是下面XAML行:
代碼段從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不起作用?
你能不能也請顯示'MappedData'資源的聲明? – Clemens 2014-10-20 20:34:04
請參閱[this](http://stackoverflow.com/a/9128855/1136211)回答,以解釋爲什麼使用更基本的集合類型可能更好。 – Clemens 2014-10-20 20:37:23
我修正了它..我做了什麼,我讓IdattMapLayer從DependencyObject繼承,它現在即使與OmegaMan的接口 – katit 2014-10-20 20:38:58