不知何故,这在运行时不起作用(但它正在编译):
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); }
}为什么?谢谢
发布于 2016-03-24 12:12:15
它不应该运行,因为您将字符串ObservableCollection的ObservableCollection<string>[]数组指定为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>()};它将给您一个设计时错误,即:
不能隐式地将类型'System.Collections.ObjectModel.ObservableCollection[]‘转换为'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>()};发布于 2016-03-24 12:11:31
类型定义为
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>()}))https://stackoverflow.com/questions/36199394
复制相似问题