MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.28k stars 2.45k forks source link

ToggleSwitch and ToggleSwitchButton works differently depending whether user clicked on it or dragged it #3458

Closed DomasM closed 5 years ago

DomasM commented 5 years ago

ToggleSwitch and ToggleSwitchButton works differently depending whether user clicked on it or dragged it

To Reproduce xaml:

<StackPanel Orientation="Vertical">
<mah:ToggleSwitch  IsChecked="{Binding IsA, Mode=TwoWay}" OnLabel="A" OffLabel="Not A"></mah:ToggleSwitch>
 <mah:ToggleSwitchButton  IsChecked="{Binding IsA, Mode=TwoWay}" >A?</mah:ToggleSwitchButton>
 <ToggleButton  IsChecked="{Binding IsA, Mode=TwoWay}" >A??</ToggleButton>
<CheckBox  IsChecked="{Binding IsA, Mode=TwoWay}" >A??</CheckBox>
 </StackPanel>

view model (PropertyChanged is injected by Fody.PropertyChanged)

 public class TestViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged ([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
        }

        private bool _IsA { get; set; }
        public bool IsA { get { return _IsA; } set { MaybeSetA (value); } }

        private void MaybeSetA (bool value) {
            var rand = new Random ();
            if (rand.NextDouble () > 0.5) {
                Trace.WriteLine ("set new value");
                _IsA = value;
            } else {
                Trace.WriteLine ("ignore new value");
            }
        }

    }

Expected behavior I expect state of the control to change depending whether MaybeSetA() decides to accept new value or not. This is so if user clicks on ToggleSwitch/ToggleSwitchButton, but not if user drags indicator. ToggleButton and CheckBox work as expected (but they are not part of MahApps). All the controls should be in the same state all the time, as they are bound to the same property. Screenshots

image

Environment(please complete the following information):

Repo https://github.com/DomasM/TestMahAppsToggleSwitch run Update-Package Fody -Reinstall after clone

DomasM commented 5 years ago

Additionally, in case of ToggleSwitch, on label and off label are shown correctly (i.e. bound to property).