Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF C#4.0
I wanted to implement the fisher-yates shuffle in wpf so I could shuffle the contents of a listview control by using a button click.
I found the follwing snippet of code but it is created for
ListView.ListViewItemCollection list = listView1.Items;
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
                int k = rng.Next(n + 1);
                ListViewItem value1 = (ListViewItem)list[k];
                ListViewItem value2 = (ListViewItem)list[n];
                list[k] = new ListViewItem();
                list[n] = new ListViewItem();
                list[k] = value2;
                list[n] = value1;
Can I modify this snippet for use in WPF? If not, could you provide a suggestion for shuffling the contents of a listview control in wpf?
Posted 5-Oct-12 8:33am
Sergey Alexandrovich Kryukov at 5-Oct-12 19:28pm
What did you try? In WPF, it's pretty much the same, for such a simple case. Just try to do it in WPF and ask a question if you face a problem. But I don't foresee a problem.
Josh Hudnell at 7-Oct-12 11:42am
Thanks for your reply Sergey,
When I tried the above snippet in wpf it would not work because ListViewItemCollection is used by windows.forms, not windows.controls which are used in wpf and I could not find an equivalent for ListViewItemCollection in wpf. Is there a way for me to access the collection of listview items in wpf using code?
Sergey Alexandrovich Kryukov at 8-Oct-12 2:53am
Of course you should never use anything from System.Windows.Forms when using WPF. What I mean is: use WPF list view, WPF list items, etc. -- everything from the WPF library, the classes analogous to that of Forms library. WPF is very different, but what you are trying to do is very similar in terms of the usage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid