WP7 TimeSpanPicker in depth
published on: 1/25/2011 | Views: N/A | Tags: C4FToolkit
by WindowsPhoneGeek
In this article I am going to talk about the TimeSpanPicker control that comes with the official release of the Coding4fun Toolkit. TimeSpanPicker is an UI elements that will automatically provide you with a TextBox input and when the user selects it, the picker will display form where you can choose another date/time using infinite scrolling. Basically this control is extended DatePicker/TimePicker that allows timespan restriction.
NOTE:For more information about the toolkit visit our previous article: Coding4Fun Toolkit for WP7 Overview and Getting Started
NOTE:For more information about the DatePicker/TimePicker controls from the Silverlight for WP7 toolkit visit this article: WP7 DatePicker and TimePicker in depth | API and Customization
Getting Started
To begin using TimeSpanPicker first add a reference to the Coding4Fun.Phone.Controls.Toolkit.dll assembly. Just create a new folder in your project and add the assembly there, after that add it as a reference to your project.
NOTE: You have to download and rebuild the Coding4Fun Toolkit project in order to generate the assembly. You can get Coding4Fun.Phone.Controls.Toolkit.dll from the the following folder: \coding4fun-61253\Coding4Fun.Phone\Coding4Fun.Phone.Controls.Toolkit\Bin\Debug
the next step is to add the "controls" prefix declaration. Make sure that your page declaration includes the "controls" namespace:
xmlns:controls="clr-namespace:Coding4Fun.Phone.Controls.Toolkit;assembly=Coding4Fun.Phone.Controls.Toolkit"
The sample code should looks like:
<controls:TimeSpanPicker/>
NOTE: To see the correct ApplicationBar icons in the TimeSpanPicker , you will need to create a folder in the root of your project called "Toolkit.Content" and put the icons in there. The toolkit provides the necessary icons, but you have to copy them from the Coding4Fun.Phone.TestApplication project. They must be named "ApplicationBar.Cancel.png" and "ApplicationBar.Check.png", and the build action must be "Content"!
Key Properties and Events
(NOTE: The only difference between TimeSpanPicker public API and DatePicker/TimePicker public API are two properties: Max and Step. For more information about the public API in details please visit the WP7 DatePicker and TimePicker in depth | API and Customization.)
Properties
- Header
- HeaderTemplate
- PickerPageUri
- Value
- ValueString
- ValueStringFormat
- Max
Max is a dependency property of type TimeSpan. It determines the max value of the TimeSpanPicker . If no value is set then there is no maximum and the TimeSpanPicker loops through its items in the following way:
From 0 to 59 for the seconds
From 0 to 59 for the minutes
From 0 to 23 for the hours
- Step
Step is a dependency property of type TimeSpan. It determines the looping step between the items in a TimeSpanPicker control, The default value is 1.
Events
- ValueChanged
Event that is invoked when the Value property changes.
Sample Usage
Example1:
<controls:TimeSpanPicker Max="0:0:6" Step="0:0:2"/>
Example2:
TimeSpanPicker timespanPicker = new TimeSpanPicker(); timespanPicker.Max = new TimeSpan(0, 0, 6); timespanPicker.Step = new TimeSpan(0, 0, 2);
Example3:
<controls:TimeSpanPicker Max="{Binding TimeSpan6Sec}" Step="{Binding TimeSpan2Sec}"/>
public MainPage()
{
InitializeComponent();
DataContext = this;
}
public TimeSpan TimeSpan6Sec { get { return TimeSpan.FromSeconds(6); } }
public TimeSpan TimeSpan2Sec { get { return TimeSpan.FromSeconds(2); } }
This means that the TimeSpanPicker is restricted between 0 seconds and 6 seconds and the looping step is 2 seconds. So the final result is the following sequence 0 - 2 - 4- 6.
Styling and Customization
Unfortunately for now you can customize only the TextBox field of the TimeSpanPicker control. If you want to customize the Full Screen popup then you will have to implement your CustomPicker page and add the looping logic on your own.
Changing the TimeSpanPicker Style
You can customize the TimeSpanPicker TextBox style either by adding some elements in the ControlTemplate of simply by changing some colors like for example:
<Style x:Key="sampleStyle" TargetType="controls:TimeSpanPicker">
<Setter Property="Background" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="YellowGreen"/>
</Style>
<controls:TimeSpanPicker Max="0:0:6" Step="0:0:2" Style="{StaticResource sampleStyle}" />
Cuztomizing the Full Screen Mode
You will have to create a Custom Picker page and ass the looping logic on your own. For more information take a look at the WP7 DatePicker and TimePicker in depth | API and Customization article. You can also take a look at another interesting custom implementation by Richard Griffin: WP7 Contrib - Customising the DateTime Picker
That was all about the TimeSpanPicker control from the Coding4fun Toolkit in details. I hope that the article was helpful.
You can find the full source code here:
You can also follow us on Twitter @winphonegeek
Comments
TimeSpanControl is great
posted by: Thimoty on 1/25/2011 5:15:38 PM
Thank you again guys! Glad to see these new controls and your in depth articles. Actually I asked a question a few weeks ago about how to implement a TimeSpan control and now after I read this post the fog begin to clear :) Looking forward to read the rest of the in depth articles.
Cheers!
Thanks a ton guys
posted by: Ramesh on 1/31/2011 9:41:36 PM
Thanks a lot guys for sharing these great articles all time.
I would like to thanks to you all. - As we are getting lot of information on this site. Regularly & keeping us up to date. I have completed my one app successfully.
In my next app I am stuck on calender control they want customize calender in that events / appointments has to set like in phone. Guys can you please help me how to proceeds on same? And if is there any default control {Which can open full calender} then how can we get that?
I would really appreciate.
Thanks!!! Waiting for next great article :)
RE:@Ramesh, Calendar control
posted by: winphonegeek on 2/1/2011 9:35:34 AM
You can use this Calendar Control: http://wpcontrols.codeplex.com/
It is a pretty cool free (open source) control.
Hope that this will help you.
Calender Control
posted by: @winphonegeek on 2/1/2011 12:07:54 PM
Thanks a lot, I will try to use it & get back to you with it's experience.
But WP7 default calender & event handling things is pretty cool, My client is asking for those.
Many Thanks.
@winphonegeek, Calender Control
posted by: Ramesh on 2/4/2011 12:35:55 PM
Thanks a lot for giving the great & correct information.
It's working very nicely.
Can you please share some more information about local storage on WP7. In my previous app I was working with Isolated Storage that works great. Now in this app we need to store the events for particular date & that needs to load on selected date, So while using Isolated Storage, application is not opening into phone if lot of events in xml file. Please help me if any other DB is supporting on WP7 like - ".SDL" etc... Where I can maintain the mapping between the objects as in iPhone - 'SQLite' is supporting.
I would really appreciate for your valuable answer :)
Regards
Ramesh
RE:local storage
posted by: winphonegeek on 2/7/2011 7:37:13 PM
Thank you for your suggestion.
We will consider writing such article as soon as possible!
@winphonegeek
posted by: Ramesh Kumar on 2/14/2011 4:51:27 PM
Thanks for your expert comment!
eagerly waiting for same -:)
Thanks!
Ramesh Kumar
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
