【发布时间】:2015-11-12 20:25:13
【问题描述】:
这可能是不可能的情况,我可能正在尝试做一些我一开始不应该做的事情,但它就是这样。
- 我有一个自定义 WPF 控件,它有两个 IEnumerable 集合
- 第一个集合 (ItemsSource) 通过 XAML 声明,可以是任何类型的对象。
- 我正在实现的第二个集合又是一个 IEnumerable,我想将其初始化为 ObservableCollection。
- 这是我的问题,因为我受到限制,两个集合都属于同一类型的对象(不,我不能将对象用作类型)。例如,ItemsSource 是“MyItem”类型的对象,我想将第二个集合初始化为 ObservableCollection()。
这可能吗?我在做我不应该做的事情吗?任何提示将不胜感激。附带说明一下,如果我通过 XAML 传递第二个集合,一切都很好,但我不想将这样的限制添加到我正在实现的功能中。
编辑:
这里有一些代码 sn-ps 来展示这个场景:
第一个集合,注意这个集合继承自System.Windows.Controls.ItemsControl类:
public IEnumerable ItemsSource { get; set; }
第二集:
public IEnumerable SelectedItems
{
get
{
this.InitializeSelectedItemsCollectionIfRequired();
return (IEnumerable)GetValue(SelectedItemsProperty);
}
set
{
SetValue(SelectedItemsProperty, value);
}
}
private void InitializeSelectedItemsCollectionIfRequired()
{
if (this.GetValue(SelectedItemsProperty) == null)
{
// Here is where I want to initialize the second collection if it was not already set in via a Binding in the XAML
this.SelectedItems = new System.Collections.ObjectModel.ObservableCollection<"dont know how to pass correct type here">();
}
}
【问题讨论】:
-
也许一些关于这两个集合的小sn-ps代码可能会有所帮助?
-
抱歉,在我的帖子中添加了几行我目前正在调试的内容,