开发者

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 changi开发者_如何学Pythonne On/Off to something else.

I want to display Yes and No.


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}" />


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 ?


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";

    }


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜