Steve Chadbourne
Steve Chadbourne

Reputation: 6953

How to customise wp7 toolkit ToggleSwitch

I have the WP7 toolkit and am using the toggle switch.

At the moment it displays On or Off.

I know you can customise it using the the content template and the sample code supplied with the tookit shows just that, but it I can't find a way of changine On/Off to something else.

I want to display Yes and No.

Upvotes: 5

Views: 3575

Answers (4)

StepTNT
StepTNT

Reputation: 3967

I know that the question is quite old, but I think that this answer may be useful because there's no need to recompile the control's code.

We can bind the Content to the IsChecked property and use a Converter that returns our custom string.

This is what I did for my project:

<toolkit:ToggleSwitch SwitchForeground="{StaticResource PhoneAccentBrush}"
                      Grid.Row="3" Grid.Column="1" 
                      Header="{Binding Path=LocalizedResources.MyLabel, Source={StaticResource LocalizedStrings}}"
                      Content="{Binding IsChecked, Converter={StaticResource SwitchOnOffConverter}, RelativeSource={RelativeSource Self}}"/>

where the SwitchOnOffConverter is this one:

public class SwitchOnOffConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {           
        return ((bool) value) ? AppResources.YesText : AppResources.NoText;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Upvotes: 2

Ben
Ben

Reputation: 21

There is a much easier way, set the content to No and then create a event handler for each toggle to make it say Yes and then No:

private void ToggleSwitch_Checked(object sender, RoutedEventArgs e)
    {
        togButton.Content = "Yes";
    }

    private void ToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
    {
        togButton.Content = "No";

    }

Upvotes: 2

Nigel Sampson
Nigel Sampson

Reputation: 10609

I created my own value converter that was bound to the same boolean property on my view model as IsChecked. So on the view it looked like:

<toolkit:ToggleSwtich IsChecked="{Binding Completed}" Content="{Binding Completed, Converter={StaticResource YesNoConverter}" />

Upvotes: 3

MatthieuGD
MatthieuGD

Reputation: 4592

Hum since the "On" et "Off" strings come from a converter set in a private method in the source code, I don't see a lot of alternative : http://silverlight.codeplex.com/SourceControl/changeset/view/55144#1325068

Change the source code to have something more flexible ?

Upvotes: 2

Related Questions