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.
This should work:
All views in the default WPF classes not only implement
ICollectionViewbut should be subclasses ofCollectionViewwhich has aCountthat can be used.