Is this the correct way to bind the IsChecked property of the ToggleSwitch to a ViewModel? RRS feed

  • General discussion

  • I have the IsChecked Property on the ToggleSwitch bound to the IsOn property on my ViewModel and see the text change in the TextBox however only see the ToggleSwitch change once, from not to is checked.  The text always changes when the property in the view model changes.

    ---- from XAML
    <StackPanel Orientation="Vertical">
             <TextBlock x:Name="ToggleSwitchDebug" Text="{Binding IsOn}"  />                <!-- this always reflects the value in the ViewModel -->
             <toolkit:ToggleSwitch x:Name="OnOffSwitch" IsChecked="{Binding IsOn}" />    <!-- this doesn't -->

    ---- from View Model
            private bool? _isOn;
            public bool? IsOn
                    Debug.WriteLine(string.Format("IsOn| OldValue: {0}, NewValue: {1}", _isOn.ToString(), value.ToString()));
                    if ( _isOn != value)
                        _isOn = value;
                get { return _isOn; }
    Wednesday, April 13, 2011 9:26 PM

All replies

  • Using your code, I couldn't get it to bind correctly either. However, it does work if you use 2-way binding, IsChecked="{Binding IsOn, Mode=TwoWay}". You probably wanted 2-way binding anyway, so that user selection of the toggle would reflect back to the view model. Using 1-way binding for a toggle might not be as useful but still should have worked for value changes initiated from code, and it looks like a bug to me.

    Richard Woo
    Thursday, April 14, 2011 8:04 AM