Click here to Skip to main content
15,891,253 members
Articles / Mobile Apps / Windows Phone 7
Technical Blog

Localize ToggleSwitch in the Silverlight Toolkit

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Jun 2013CPOL 10.1K   1   1
Localize ToggleSwitch in the Silverlight Toolkit.

I've read tons of blogs and questions and answers, but cannot get to localize the ToggleSwitch in the Silverlight Toolkit in my Windows Phone application. So I decided to get my own way. My proposal is based on a DataTemplate for ToggleSwitch and ValueConverter. So let's go.

First add a Localized Resource for our new On and Off string values:

Image 1

The next step is to create the ValueConverter:

C#
public class BoolToSwitchConverter : IValueConverter  
{  
    private string FalseValue = Resources.Off;  
    private string TrueValue  = Resources.On;  
  
    public object Convert(object value, Type targetType, 
           object parameter, System.Globalization.CultureInfo culture)  
    {  
        if (value == null)  
            return FalseValue;  
        else  
            return ("On".Equals(value)) ? TrueValue : FalseValue;  
    }  
  
    public object ConvertBack(object value, Type targetType, 
           object parameter, System.Globalization.CultureInfo culture)  
    {  
        return value != null ? value.Equals(TrueValue) : false;  
    }  
}

Make our converter visible from XAML, adding glue code to Application Resources in App.xaml. Add a link to the app namespace if you do not have it already, do this before xmlns:local="clr-namespace:AppNamspace". And add the resource code:

XML
<Application.Resources>  
    <local:BoolToSwitchConverter x:Key="Switch" />  
</Application.Resources>

And finally override the DataTemplate on our ToggleSwitch:

XML
<toolkit:ToggleSwitch x:Name="MySwitch" Header="Localized Switch">  
    <toolkit:ToggleSwitch.ContentTemplate>  
        <DataTemplate>  
            <ContentControl HorizontalAlignment="Left"   
                Content="{Binding Converter={StaticResource Switch}}"/>  
        </DataTemplate>  
    </toolkit:ToggleSwitch.ContentTemplate>  
</toolkit:ToggleSwitch>

Result:

Image 2

License

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


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
QuestionNot necessary any more - but might be still useful for some Pin
gebrudergrimm30-Jun-14 1:45
gebrudergrimm30-Jun-14 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.