Home > Uncategorized > Localise DatePicker in #WP8 #SilverlightToolkit using hooks

Localise DatePicker in #WP8 #SilverlightToolkit using hooks

The Silverlight toolkit for Wndows phone is really handy to get up and running quickly with advanced functionality on a Windows Phone app, however, when you want to customize it, you find that you end up downloading the source, and tinkering with it directly, which is fine, since it’s an open source project. However, for a minor change, it would be nice to keep treating it as a black box.

This approach however, could therefore be used to access and modify the XAML of any control, even closed-source ones. Which could be useful for Syncfusion or Telerik controls too.

So, first, we modify app.xaml.cs to create our hook.

Comment out // RootFrame.Navigated -= CompleteInitializePhoneApplication; in CompleteInitializePhoneApplication, so that the method gets called on every page load, not just on first load.

Then check the current page like this

PhoneApplicationPage page = RootFrame.Content as PhoneApplicationPage;
var strPage = (sender as NavigationService).CurrentSource.ToString();
if (strPage.Contains(“DatePickerPage.xaml”)) UI.DatePickerHook(page);

Hopefully, you don’t have a page called DatePickerPage.xaml in your app!, but this will call UI.DatePickerHook when the datepicker is opened.

Now, for a helpful utility function, which I’ve called LoopThroughControls – which applies an action to every control on a page recursively as follows;

private static void LoopThroughControls(UIElement parent, Action<UIElement> modifier)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
modifier(child);
LoopThroughControls(child, modifier);
}
}
return;

Then the DatePickerHook function is defined as follows

public static void DatePickerHook(PhoneApplicationPage page)
{
// Somehow modify the text on the top of the page…
LoopThroughControls(page, (ui => {
var tb = ui as TextBlock;
if (tb != null && tb.Name == “HeaderTitle”)
{
tb.Text = “”;
}
}));
}

Categories: Uncategorized

Leave a comment