简体   繁体   中英

ToggleButton IsChecked Not Adhering to Bound Property

So I have a toggle button as follows:

  <ToggleButton 
      IsChecked="{Binding IsButtonChecked, Mode=OneWay}"
      Command="{Binding DoNothing}"
      CommandParameter="{Binding ServerViewModel}"
      Content="Click Me!"></ToggleButton>

The initial value of IsButtonChecked = false

When I click the toggle button the ICommand properly fires (which is bound to a RelayCommand ) and this command returns false for CanExecute. The state of the WPF ToggleButton is now Checked=true, however the backed model is still IsButtonChecked = false . Why did the UI update to a checked state, even though the bound property hasn't?

Side Note

The only way I've been able to prevent the UI from updating is creating an inverse property IsButtonNotChecked . I then bind that property to IsEnabled in the XAML. This prevents button clicks from occurring if the state is currently enabled.

You have the binding mode set to OneWay , set it to TwoWay .

<ToggleButton Command="{Binding DoNothing}"
              CommandParameter="{Binding ServerViewModel}"
              Content="Click Me!"
              IsChecked="{Binding IsButtonChecked,
                                  Mode=TwoWay}" />

For what its worth, here's what I did.. It looks really clunkly.

I set the binding mode to TwoWay. It didn't look like OneWay binding respected the IsChecked property.

  <ToggleButton 
      IsChecked="{Binding IsButtonChecked, Mode=TwoWay}"
      Command="{Binding DoNothing}"
      CommandParameter="{Binding ServerViewModel}"
      Content="Click Me!"></ToggleButton>

Secondly, I no-opp'ed the mutator property of IsButtonChecked.

 public bool IsButtonChecked
        {
            get
            {
                return _isButtonChecked;
            }
            set
            {
                // Prevents the IsButtonCheckedfrom incorrectly being set to a
                // enabled state, yet the model is false
                // IsButtonCheckeddoesn't seem to respect OneWay binding.               
            }
        }

Then within code behind, I update the _isButtonChecked property and call the INotifyPropertyChanged event.

internal void ShouldBeChecked(bool isChecked)
{ 
_isButtonChecked = isChecked;
 OnPropertyChanged("IsButtonChecked"); 
}

Really clunky though... Its odd that a ToggleButton doesnt respect the bound property...

As explained in my answer here just raise the PropertyChanged Event for your IsButtonChecked Property as first action in your DoNothing Command. With this approach one doesn't need to distort the bound property.

Please check the referenced answer for more details if needed. Added this answer for completeness to lead people finding this question to a possibly less clunky solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM