2010-07-02 81 views
1

我试图创建一个自定义列表,其中包含对象列表。虽然在Xaml和后面的代码中设置了这个,但我得到了“错误的标记错误”。以下是我如何设置PropertyKey和Property。无法创建集合类型依赖项属性

private static readonly DependencyPropertyKey ItemSourcePropertyKey = DependencyProperty.RegisterReadOnly(
     "ItemSource", 
     typeof(List<object>), 
     typeof(MultiSelectComboBox), 
     new FrameworkPropertyMetadata(null)); 

public static readonly DependencyProperty ItemSourceProperty = ItemSourcePropertyKey.DependencyProperty; 

领域是:

public List<object> ItemSource 
    { 
     get { return (List<object>)this.GetValue(ItemSourceProperty); } 
     set { this.SetValue(ItemSourceProperty, value); } 
    } 

我得到的错误是:

{"Cannot create instance of 'Window1' defined in assembly 'GroupExpansion, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Window1.xaml' Line 1 Position 9."} 

有没有人约束一个DependencyProperty之前的列表?我遇到了一些教程,但是当我尝试模拟教程时,它就落在了它的脸上。列表和属性位于扩展UserControl的文件中。如果您需要更多的细节,请询问。现在我已经把头从键盘上敲了几天。好像是围着圈子走。

干杯

+0

上面提到的是否存在内部异常? – 2010-07-02 13:59:51

+0

是非常有帮助的:{“调用的目标引发异常”},源代码是:“PresentationFramework”。哦,我注意到我遗漏了在类构造函数中完成的属性的构造函数,并为它分配一个空的列表 Tim 2010-07-02 14:16:45

+0

啊哈,多一点信息。我将列表更改为ObservableCollection,然后在InnerException中产生以下错误:'ItemSource'属性被注册为只读,并且无需授权密钥即可修改。现在寻找一种解决方法。 – Tim 2010-07-02 14:53:43

回答

1

找到了解决办法,这似乎是依赖属性需要被连接到的ObservableCollection。然后绑定它然后只需要非读取,因此DependencyPropertyKey可以被删除并且只替换为DependencyProperty。如果有人为此设置实际页面,这将会非常有用。