可观察收集的可观察收集声明

时间:2016-03-24 11:44:42

标签: c# wpf observablecollection dependency-properties

不知何故,这不是在运行时工作(但它正在编译):

    public static readonly DependencyProperty SelectedVideoFileNamesProperty =
    DependencyProperty.Register("SelectedVideoFileNames", typeof(ObservableCollection<ObservableCollection<string>>), typeof(CMiX_UI), 
        new PropertyMetadata(new[]{new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>()}));
    [Bindable(true)]
    public ObservableCollection<ObservableCollection<string>> SelectedVideoFileNames
    {
        get { return (ObservableCollection<ObservableCollection<string>>)this.GetValue(SelectedVideoFileNamesProperty); }
        set { this.SetValue(SelectedVideoFileNamesProperty, value); }
    }

为什么?谢谢

2 个答案:

答案 0 :(得分:1)

您的类型定义为

ObservableCollection<ObservableCollection<string>>

然而,您将默认设置为:

new[]{new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()}))

哪个是可观察集合的数组。你需要:

new ObservableCollection<ObservableCollection<String()>> {new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()}))

答案 1 :(得分:1)

当您将字符串ObservableCollection<string>[]的ObservableCollection数组指定为ObservableCollection<ObservableCollection<string>>的默认值时,它不应该正在运行。如果您编写如下代码:

        ObservableCollection<ObservableCollection<string>> o = new[]{new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()};

它会给你一个设计时错误:

  

无法隐式转换类型&#39; System.Collections.ObjectModel.ObservableCollection []&#39;到&#39; System.Collections.ObjectModel.ObservableCollection&gt;&#39;

相反,你可以:

        ObservableCollection<ObservableCollection<string>> o = new ObservableCollection<ObservableCollection<string>> {new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()};