the line i got the error :
IEnumerator enumerator = this.ListBox1.Items.GetEnumerator();
while (enumerator.MoveNext())
{
object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
list2.Add(Conversions.ToString(objectValue));
}
the error is in the first line.
error i got :
Error 1 'System.Windows.Data.CollectionView.GetEnumerator()' is inaccessible due to its protection level
how can i fix it ?
thx for the helpers !
MahApps wpf app
The simplest fix would be to use the language support for iterators:
foreach (object value in ListBox1.Items)
{
list2.Add(Conversions.ToString(value));
}
If you expect the values to already be strings, you could make the foreach
loop cast for you implicitly:
foreach (string value in ListBox1.Items)
{
list2.Add(value);
}
Or if you're creating a new list anyway, you could use a one-liner:
var list2 = ListBox1.Items.Cast<string>().ToList();
The reason your current code doesn't work is that CollectionView
uses explicit interface implementation for IEnumerable
. You could change your code to just:
IEnumerator enumerator = ((IEnumerable)this.ListBox1.Items).GetEnumerator();
... but it would be much better to use one of the approaches above.
See more on this question at Stackoverflow