Continuing our RegistrationBooth Application…
In most of the offline events we hold, there are some giveaways for the audience (most of the time the prize is XBOX). Picking a winner is manual process where we collect the feedback forms, put it in a big box/bag, shake the box/bag and turn it upside down multiple times to make sure that the forms are scrambled enough so that when we pick a form it will be a random one , not the best process I know.
So in this post we will add to our RegistrationBooth application the ability to perform a random draw. we already have our attendees listed in a PathListBox, wouldn’t it be nice if this list can spin like a roulette till it stops at the lucky winner, let’s see how we can implement this. I thought it’s better if we implement this feature as a behavior so that we can easily apply it to any PathListBox, I will call this behavior RouletteBehavior.
To create a behavior we need to derive from the abstract class Bahavior<T> available in the System.Windows.Interactivity assembly, where T is the control or UI Element that you will attach this behavior to. in our case our behavior will derive from Bahavior<PathListBox>
What properties this behavior will expose? first there should be a way to start the roulette, for maximum usability we will add a property StartCommand of type ICommand, so that users of this behavior can use any type of trigger to start the roulette
We need another property to determine how long the roulette will spin, so we will add a property Duration of type Duration, I will make this property a DependencyProperty to make it open for any advanced scenario that the users of this behavior might want to implement.
To be able to customize the spinning speed we will expose a property called Speed (of type integer) that determines the speed of spinning it terms of items/second
There are two protected method that we need to override, OnAttached and OnDetaching these methods are called when attaching and detaching the behavior to a PathListBox, the AssociatedObject property of the Behavior gives you access to the PathListBox you are attaching the behavior to, so what we do is we change the WrapItems property of the PathListBox when attaching the behavior and we set it back to the original value when detaching the behavior
To create the spinning animation we will need a storyboard, so we will add a member variable of type Storyboard to our behavior class and in the constructor we will initialize the storyboard and attach a method called StartRoulette to the StartCommand
The StartRoulette method contains the logic for animating the PathListBox items, we do this by creating a DoubleAnimation to animate the Start property of the LayoutPath of the PathListBox (currently this behavior supports only one layout path), based on the list items count, the Speed property, the Duration and a generated random number we calculate the final value of the Start property that we will assign to the To property of the animation.
We assign a Circle out easing function to the animation so that the speed of spinning is decreased over the duration of the animation till it stops.
If we try this now the items will spin (the items are not actually spinning we are changing Start property of the layout path and that makes it look like spinning). but we didn’t specify yet how we will select the winning item, so what we need to do is that while the items are virtually spinning we need to change the selected item or more specifically change the SelectedIndex property. The problem is that we need to keep changing the SelectedIndex value as long as the Start property value is changing, how we do this by binding the two properties. so we will add the following code to the OnAttached method
The code binds the SelectedIndex property to the Start property of the first LayoutPath (this behavior supports 1 LayoutPath), we need a custom IValueConverter to convert the value of the Start property to a correct SelectedIndex property that’s where the StartToIndexConverter comes into play, the two functions that we need to implement in the IValueConverter interface are shown below
Basically what this conversion does is that if the Start value is increased/decreased by an amount that’s greater than the spacing between the items, the converter increments/decrements the SelectedIndex value to move to the next/previous item. we need to make sure that SelectedIndex value remains within the range of the items that are actually visible on the Path that’s why we are checking the PathListBoxItem.IsArranged property.
Once we finish the Behavior, you can drag it from the Assets window and drop it on your PathListBox, then you can change its properties accordingly
PS: This behavior will not work correctly if you are using PathListBoxScrollBehavior.ScrollSelectedCommand
You can download the RegistrationBooth from here.
And you can download a Demo for using the RouletteBehavior from here