1

Closed

IsChecked property loses binding

description

When you toggle the control with the mouse, the IsChecked property changes (as you'd expect), but anything bound to the isChecked property loses its binding.

In ToggleSwitchBase.cs OnClick() method, you shouldn't set IsChecked directly. You should use this approach:
this.SetCurrentValue(IsCheckedProperty, !IsChecked);
This changes the value of IsChecked, without removing any bindings on the dependency property.

The description of the SetCurrentValue method says:
This method is used by a component that programmatically sets the value of one of its own properties without disabling an application's declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.
See SetCurrentValue
Closed Sep 8, 2015 at 9:10 PM by EricJensen

comments

A_Daborn wrote Jun 19, 2015 at 1:57 PM

A workaround until this is fixed (if it's fixed) is to manually re-bind the property whenever the IsChecked property is set.

It's pretty simple. Add an event for the IsChecked property, and add this code:
private void toggleSwitch_Checked(object sender, RoutedEventArgs e)
        {
            // This control loses the binding every time it's clicked!

            // Manually replace the binding
            Binding binding = new Binding();
            binding.Path = new PropertyPath("MyViewModelProperty");
            binding.Source = DataContext;
            binding.Mode = BindingMode.TwoWay; // important
            toggleSwitchTutorial.SetBinding(ToggleSwitch.HorizontalToggleSwitch.IsCheckedProperty, binding);
        }
Of course, you could create the binding once, and re-apply it to the control within this event, which would be more efficient.

EricJensen wrote Jun 27, 2015 at 9:40 PM

Thank you for submitting the issue. Unfortunately I no longer have a Windows development environment setup to verify the fix. I have added you as contributor to the project. You should now be allowed to push your fix to codeplex.

wrote Jun 27, 2015 at 9:43 PM

wrote Sep 8, 2015 at 9:10 PM