- Create your DatePicker application described in this article or your own application.
- Insert in MainPage.xaml.cs
using System.Globalization; using System.Threading;
- Windows Phone supports the display languages you may find here
- Next code fragment do all you need (of course you must choose "Culture code" you need):
public MainPage() { CultureInfo newCulture = new CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = newCulture; Thread.CurrentThread.CurrentUICulture = newCulture; ... } - But if your language not supported by windows phone?
public MainPage() { CultureInfo newCulture = new CultureInfo("en-GB"); //change format of DatePicker to you own format newCulture.DateTimeFormat.ShortDatePattern = "d/MMM/yyyy"; //set day names newCulture.DateTimeFormat.DayNames = new string[] { "first day", "second day", "third day", "4 day", "5 day", "6 day", "7 day" }; //set month names newCulture.DateTimeFormat.MonthNames = new string[] {"1 month", "2 month", "3 month", "4 month", "5 month", "6 month", "7 month", "8 month", "9 month", "10 month", "11 month", "12 month", ""}; Thread.CurrentThread.CurrentCulture = newCulture; Thread.CurrentThread.CurrentUICulture = newCulture; ... } - Enjoy
Showing posts with label Date Picker. Show all posts
Showing posts with label Date Picker. Show all posts
WP7: How to Localize you DatePicker
WP7 - Create DatePicker control programmatically
If you need use DatePicker in your windows phone application you must create control field in xaml file. Or no...
Let's create DatePicker control programmatically without any code in xaml file.
Let's create DatePicker control programmatically without any code in xaml file.
- Install Silverlight Toolkit last version.
- Create new "Windows Phone Application" named "DatePickerControlProgrammatically".
- Adding new reference to Microsoft.Phone.Controls.Toolkit
- Main Menu -> Project -> Add Reference -> Tab ".NET" -> choose Microsoft.Phone.Controls.Toolkit.
- If you no find Microsoft.Phone.Controls.Toolkit in ".NET" tab go to "Browse" tab and navigate to this dll in you computer (for me "C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Sep10\Bin\Microsoft.Phone.Controls.Toolkit.dll").
- Create new folder in project "Toolkit.Content" and put inside two images for cancel and check control (you can get them in Silverlight Toolkit project or create you own icons):
- Don't forget change "Build Action" property for each image to "Content" from "Resource".
- Override DatePicker class with our custom DatePickerCustom class. Create new class "DatePickerCustom.cs":
using System.Windows.Automation.Peers; using System.Windows.Automation.Provider; using System.Windows.Controls; using Microsoft.Phone.Controls; namespace DatePickerControlProgrammatically { public class DatePickerCustom : DatePicker { public void ClickTemplateButton() { Button btn = (GetTemplateChild("DateTimeButton") as Button); ButtonAutomationPeer peer = new ButtonAutomationPeer(btn); IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider); provider.Invoke(); } } } - MainPage.xaml.cs - create new DatePickerCustom on Loaded event:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace DatePickerControlProgrammatically { public partial class MainPage : PhoneApplicationPage { private DatePickerCustom datePicker; // Constructor public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { // create datePicker programmatically if (this.datePicker == null) { this.datePicker = new DatePickerCustom(); this.datePicker.IsTabStop = false; this.datePicker.MaxHeight = 0; this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged); LayoutRoot.Children.Add(this.datePicker); } } void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e) { // now we may use got value from datePicker PageTitle.Text = this.datePicker.ValueString; } } } - For test add to MainPage.xaml two buttons - "Get Date" and "Clear Date":
<Button Content="Get Date" Height="72" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click"/> <Button Content="Clear Date" Height="72" HorizontalAlignment="Right" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click"/>
- And to MainPage.xaml.cs add control for our buttons:
private void button1_Click(object sender, RoutedEventArgs e) { this.datePicker.ClickTemplateButton(); } private void button2_Click(object sender, RoutedEventArgs e) { PageTitle.Text = "Page Title"; clearDatePicker(); } void clearDatePicker() { this.datePicker.ValueChanged -= new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged); //this.datePicker.Value = "8/16/2011"; this.datePicker.Value = null; this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged); } - Run
Subscribe to:
Posts (Atom)
