我如何 select 来自 IEnumerable 的元素将它们存储在另一个 IEnumerable 中?
How can i select elements from an IEnumerable to store them in another IEnumerable?
我已经构建了一个自定义用户控件,它的输入是一个 IEnumerable,它应该 return 也是一个 IEnumerable。这是为了有一个灵活的控件,可以接收任何对象的集合。这里有一些代码片段可以帮助您理解我的问题:
项目来源
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MultiSelectionComboBox), new PropertyMetadata(
new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
已选项目
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(MultiSelectionComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(MultiSelectionComboBox.OnSelectedItemsChanged)));
public IEnumerable SelectedItems
{
get { return (IEnumerable)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
除了构建 SelectedItems 的这一部分,我能够让我的控制工作 属性
foreach(string s in appo)
{
IEnumerator en = ItemsSource.GetEnumerator();
while (en.MoveNext())
{
var val = en.Current;
Type type = val.GetType();
PropertyInfo property = type.GetProperty(DisplayMemberPath);
if (property != null)
{
string name = (string)property.GetValue(val, null);
if(name == s)
{
// need something here
}
}
}
}
基本上在 if
内部,我检查过 IEnumerator
en 的当前元素必须包含在 SelectedItem 中。问题是我不知道如何在输出中包含这个元素(即 SelectedItems)。
如果你有更好的想法,我也对不同的方法持开放态度
问题是 IEnumerable
没有添加项目的能力。因此,您需要将其更改为具有 Add 方法的内容,例如 IList
或 ICollection<object>
.
我已经构建了一个自定义用户控件,它的输入是一个 IEnumerable,它应该 return 也是一个 IEnumerable。这是为了有一个灵活的控件,可以接收任何对象的集合。这里有一些代码片段可以帮助您理解我的问题:
项目来源
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MultiSelectionComboBox), new PropertyMetadata( new PropertyChangedCallback(OnItemsSourcePropertyChanged))); public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } }
已选项目
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(MultiSelectionComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(MultiSelectionComboBox.OnSelectedItemsChanged))); public IEnumerable SelectedItems { get { return (IEnumerable)GetValue(SelectedItemsProperty); } set { SetValue(SelectedItemsProperty, value); } }
除了构建 SelectedItems 的这一部分,我能够让我的控制工作 属性
foreach(string s in appo)
{
IEnumerator en = ItemsSource.GetEnumerator();
while (en.MoveNext())
{
var val = en.Current;
Type type = val.GetType();
PropertyInfo property = type.GetProperty(DisplayMemberPath);
if (property != null)
{
string name = (string)property.GetValue(val, null);
if(name == s)
{
// need something here
}
}
}
}
基本上在 if
内部,我检查过 IEnumerator
en 的当前元素必须包含在 SelectedItem 中。问题是我不知道如何在输出中包含这个元素(即 SelectedItems)。
如果你有更好的想法,我也对不同的方法持开放态度
问题是 IEnumerable
没有添加项目的能力。因此,您需要将其更改为具有 Add 方法的内容,例如 IList
或 ICollection<object>
.