简体   繁体   English

ToggleButton IsChecked 不遵守绑定属性

[英]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 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.当我单击切换按钮时, ICommand 正确触发(绑定到RelayCommand )并且此命令为 CanExecute 返回 false。 The state of the WPF ToggleButton is now Checked=true, however the backed model is still IsButtonChecked = false . WPF ToggleButton 的 state 现在是 Checked=true,但是支持的 model 仍然是IsButtonChecked = false Why did the UI update to a checked state, even though the bound property hasn't?为什么 UI 更新为选中的 state,即使绑定的属性没有?

Side Note边注

The only way I've been able to prevent the UI from updating is creating an inverse property IsButtonNotChecked .我能够阻止 UI 更新的唯一方法是创建一个反向属性IsButtonNotChecked I then bind that property to IsEnabled in the XAML. This prevents button clicks from occurring if the state is currently enabled.然后,我将该属性绑定到 XAML 中的IsEnabled 。如果当前启用了 state,这将防止发生按钮点击。

You have the binding mode set to OneWay , set it to TwoWay . 您已将绑定模式设置为OneWay ,将其设置为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. 我将绑定模式设置为TwoWay。 It didn't look like OneWay binding respected the IsChecked property. 看起来OneWay绑定不尊重IsChecked属性。

  <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. 其次,我没有选择IsButtonChecked的mutator属性。

 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. 然后,在后面的代码中,我更新_isButtonChecked属性并调用INotifyPropertyChanged事件。

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

Really clunky though... Its odd that a ToggleButton doesnt respect the bound property... 虽然真的很笨拙。ToggleButton不尊重绑定属性,这很奇怪。

As explained in my answer here just raise the PropertyChanged Event for your IsButtonChecked Property as first action in your DoNothing Command.正如我在此处的回答中所解释的,只需将IsButtonChecked属性的PropertyChanged事件作为DoNothing命令中的第一个操作。 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.为了完整性而添加了这个答案,以引导人们找到这个问题,找到一个可能不那么笨拙的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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