0

I'm writing an application which reads data from an XML file and I use previous/next buttons to cycle through each record. However, I'd like to know how to use a button to select a random record. My Previous/Next buttons are as follows:

private void Next_Click(object sender, RoutedEventArgs e)
{
        var button = (Button)sender;
        var cvs = (CollectionViewSource)button.Tag;
        cvs.View.MoveCurrentToPrevious();

        if (cvs.View.IsCurrentBeforeFirst)
        {
            cvs.View.MoveCurrentToLast();
        }
}

private void Previous_Click(object sender, RoutedEventArgs e)
{
        var button = (Button)sender;
        var cvs = (CollectionViewSource)button.Tag;
        cvs.View.MoveCurrentToNext();

        if (cvs.View.IsCurrentAfterLast)
        {
            cvs.View.MoveCurrentToFirst();
        }
}

private void Random_Click(object sender, RoutedEventArgs e)
{

}

I've searched everywhere for a solution, including Microsoft documentation (http://msdn.microsoft.com/en-us/library/system.random.aspx) but I'm still stumped. Any help will be greatly appreciated.

2 Answers 2

0

This should work:

readonly Random _random = new Random();
private void Random_Click(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;
    var cvs = (CollectionViewSource)button.Tag;
    var view = cvs.View as CollectionView;
    if (view != null)
    {
        cvs.View.MoveCurrentToPosition(_random.Next(0, view.Count));
    }
}

All views in the default WPF classes not only implement ICollectionView but should be subclasses of CollectionView which has a Count that can be used.

5
  • Thanks very much again H.B. It did baffle me as to why CollectionViewSource didn't have a Count. However, do you how to generate the next number, without it including the current number? I don't seem to be able to find a solution. Many thanks.
    – duney
    Nov 7, 2011 at 11:19
  • @duney: What do you mean by current number? The above code does not use a current number, it just specifies upper and lower bounds.
    – brunnerh
    Nov 7, 2011 at 12:00
  • Sorry, that probably didn't make much sense. Well, the CollectionView number is set to the random number right? the problem I'm having is that sometimes when I press random, it will randomly select the same number as the index of the xml record that is currently shown on the screen, meaning that it doesn't change to a different record...just the same one.
    – duney
    Nov 7, 2011 at 13:04
  • @duney: Just create a field for the index that was randomly chosen and in which you save the random value and place a while loop around a random number generation which only moves on when a different number is generated. This is pretty basic stuff really...
    – brunnerh
    Nov 7, 2011 at 13:50
  • I don't know why I didn't even think of that. Lack of experience I guess. Thanks :-)
    – duney
    Nov 7, 2011 at 13:59
0

System.Random is what you'll need to generate the random number, but you also need to figure out how many total records are in your CollectionView to get a range for the random number gen.

Something along these lines:

private void Random_Click(object sender, RoutedEventArgs e)
{
    var count = CollectionView.Count;
    var random = new System.Random();
    var index = random.next(0, count);
    CollectionViewSource.View.MoveCurrentToPosition(index);
}
1
  • The upper bound of Random.Next is exclusive by the way, so - 1 might not be such a good idea.
    – brunnerh
    Nov 6, 2011 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.