Question

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?

Was it helpful?

Solution

Try this:

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top