C# 无法创建集合类型依赖项属性

C# 无法创建集合类型依赖项属性,c#,list,dependency-properties,C#,List,Dependency Properties,我正在尝试创建一个自定义列表,其中包含一个对象列表。虽然当在Xaml和代码中都设置了这一点时,我会得到一个“错误的标记错误”。下面是我如何设置PropertyKey和Property的 private static readonly DependencyPropertyKey ItemSourcePropertyKey = DependencyProperty.RegisterReadOnly( "ItemSource", typeof(List<objec

我正在尝试创建一个自定义列表,其中包含一个对象列表。虽然当在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;
以前是否有人将DependencyProperty绑定到列表?我遇到过一些教程,但当我试图模仿这些教程时,它就落到了我的脸上。列表和属性位于扩展UserControl的文件中。如果你需要更多的细节,尽管问。这几天我一直在用键盘敲脑袋。似乎在兜圈子


Cheers

找到了解决方案,似乎依赖项属性需要附加到ObservableCollection。然后对于绑定,它需要是非只读的,这样DependencyPropertyKey就可以被删除并替换为DependencyProperty。如果有人为此设置了一个实际页面,这将非常有用。

找到了解决方案,似乎需要将Dependency属性附加到ObservableCollection。然后对于绑定,它需要是非只读的,这样DependencyPropertyKey就可以被删除并替换为DependencyProperty。如果有人为此设置了一个实际页面,这将非常有用。

您上面提到的页面是否存在内部异常?是的,非常有用:{“异常已由调用的目标引发。”},源代码是:“PresentationFramework”。哦,我注意到我遗漏了属性的构造函数,它是在类构造函数中完成的,并为itAh ha分配了一个空列表,这是更多的信息。我将列表更改为ObservableCollection,然后在InnerException中产生以下错误:“ItemSource”属性注册为只读,没有授权密钥无法修改。现在来寻找一种解决方法。你上面提到的有内部异常吗?是的,非常有用:{“异常是由调用的目标抛出的。”},源代码是:“PresentationFramework”。哦,我注意到我遗漏了属性的构造函数,它是在类构造函数中完成的,并为itAh ha分配了一个空列表,这是更多的信息。我将列表更改为ObservableCollection,然后在InnerException中产生以下错误:“ItemSource”属性注册为只读,没有授权密钥无法修改。现在我们来寻找一种方法来解决这个问题。
 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."}