Pregunta

I want to manipulate all entries in a ListView, preferably in parallel.

Parallel.Foreach<ListViewItem>(auftraegeView.Items, line => 
{
    line.Font = new Font(line.Font.FontFamily, someInt);
});

That refuses to work though, because

cannot convert from ListViewItemCollection to IEnumerable

ListViewItemCollection doesn't offer methods like ToList() or such.
I a sort of for-the-laughs-and-giggles-attempt I made a cast

(IEnumerable<ListViewItem>)auftraegeView.Items

That didn't throw a Compiler-Error, but caused an InvalidCasTException at runtime.

Note:
The pointy brackets are inversered intentionally so they don't get eaten up as HTML here.

Question:
Is there a way to convert ListView.Items / ListViewItemCollection to something that implements the IEnumerable-Interface, hence works with Parallel.ForEach?

¿Fue útil?

Solución

Try this:

Parallel.Foreach(auftraegeView.Items.Cast<ListViewItem>(), line => 
{
    line.Font = new Font(line.Font.FontFamily, someInt);
});

Otros consejos

Your question was probably answered, but maybe you should consider another way how to set FontFamily of lines. It should be UI stuff. Try to declare it in styles or if you want do change font dynamically, use some kind of triggers.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top