Reputation: 1703
I have built a custom User Control, that has as input an IEnumerable, and it should return also an IEnumerable. This is to have a flexible control that can receive any collection of objects. Here is some code snippets to help you understand my problem:
Item Source
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); }
}
Selected Items
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);
}
}
I was able to have my control work except for this part that Build the SelectedItems property
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
}
}
}
}
Basically inside the if
i have checked that the current element of the IEnumerator
en must be included in the SelectedItem. The problem is that i don't know how i can include this element in the output (that is SelectedItems).
I am open also to different approach, if you have better ideas
Upvotes: 0
Views: 138
Reputation: 4679
The issue is that IEnumerable
does not have the ability to add items. You therefore need to change it to be something that has an Add method, IList
for example, or ICollection<object>
.
Upvotes: 3