Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / C#

Silverlight for Windows Phone Toolkit is Loopy

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Sep 2010CPOL1 min read 10K  
Silverlight for Windows Phone Toolkit is Loopy

Loopy - Adj. describing a state of goofiness usually occurring after a long night of partying or any other activity that provokes sleep deprivation.

I started playing with the Silverlight for Windows Phone Toolkit and I absolutely love it! One of the cool controls they have is this Date & Time picker! At the heart of this control is a LoopingSelector! I created a simple demo of how to reuse this control…

The scenario is as follows… Let's assume that I have a simple application where my user needs to plan their exercise program. Thy first need is to select the type of exercise but then for each set, they have to select the amount of repetitions they want to do and then the weight they will be using…

To make the LoopingSelector work, I first need a data source… I created a very generic IntRangeDataSource:

C#
class IntRangeDataSource : ILoopingSelectorDataSource
{
    public int Minimum { get; set; }
    public int Maximum { get; set; }
    public int IncrementWith { get; set; }

    public IntRangeDataSource()
    {
        Minimum = 0;
        Maximum = 100;
        IncrementWith = 1;
        SelectedItem = 0;
    }

    public object GetNext(object relativeTo)
    {
        int value = (int)relativeTo;
        if (value >= Maximum)
            return Minimum;

        return value + IncrementWith;
    }

    public object GetPrevious(object relativeTo)
    {
        int value = (int)relativeTo;
        if (value <= Minimum)
            return Maximum;

        return value - IncrementWith;
    }

    private int selectedItem = 0;
    public object SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            if (selectedItem != (int)value)
            {
                selectedItem = (int)value;
            }                
        }
    }

    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
}

To not make the demo too complicated, I just added two LoopingSelectors to my main form… in a REAL application, this should be encapsulated in a user control.

XML
<p:LoopingSelector Width="148" ItemSize="148, 148" ItemMargin="6" x:Name="selector1" />
<p:LoopingSelector Width="148" ItemSize="148, 148" ItemMargin="6" x:Name="selector2" />

And finally, hook up the DataSource:

C#
selector1.DataSource = new IntRangeDataSource() { SelectedItem = 15 };
selector2.DataSource = new IntRangeDataSource() 
	{ SelectedItem = 50, IncrementWith = 5 };

And that's it…

Cool, huh?

If you want to learn more about these controls, check out this video: Inside Windows Phone #05-Windows Phone Silverlight Toolkit.

Dave, David, and Jaime

License

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


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --