observablecollection 声明的 observablecollection

observablecollection of observablecollection declaration

不知何故,这在运行时不起作用(但正在编译):

    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); }
    }

为什么?谢谢

您的类型定义为

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>()}))

它不应该是 运行,因为您将字符串 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>()};

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

Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection[]' to 'System.Collections.ObjectModel.ObservableCollection>'

相反,您可以:

        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>()};