对象不能转换为类型MyCustomType
问题描述:
可能很简单的东西,但无法弄清楚,为什么我得到这个错误..对象不能转换为类型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不起作用?
答
由于使用反射在幕后执行绑定,需要一定的层次结构来完成所有工作,因此绑定对于接口不太合适。
将您的控件MapItemsDataSource
依赖项属性更改为遵守有问题的接口的实际实例的可观察集合。
目前还不清楚这是否针对最新版本的Silverlight进行了更改,但有人在S4上为此线程的接口写了一个动态绑定的包装器。
Binding to an interface where the implementation class is internal. (Silverlight 3)
你能不能也请显示'MappedData'资源的声明? – Clemens 2014-10-20 20:34:04
请参阅[this](http://*.com/a/9128855/1136211)回答,以解释为什么使用更基本的集合类型可能更好。 – Clemens 2014-10-20 20:37:23
我修正了它..我做了什么,我让IdattMapLayer从DependencyObject继承,它现在即使与OmegaMan的接口 – katit 2014-10-20 20:38:58