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?

有帮助吗?

解决方案

Try this:

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top