Click here to Skip to main content
12,894,700 members (60,975 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# 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
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 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?
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100