为什么不能做单向绑定到切换按钮物业器isChecked? [英] Why could not do OneWay binding to ToggleButton IsChecked property?

查看:214
本文介绍了为什么不能做单向绑定到切换按钮物业器isChecked?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换按钮支持ICommand的,所以我创造了许多命令,TogglePlayPause,ToggleMute它做工精细,但我需要财产器isChecked绑定过所以它的选中状态始终显示正确的状态。但是当我创建一个切换按钮单向绑定方式,当我preSS切换按钮后,就会失去约束力。

ToggleButton support ICommand, so I create many command such TogglePlayPause, ToggleMute and it work fine but i need to bind IsChecked property too so its checked state always show correct state. but when I create OneWay binding mode for ToggleButton and when I Press ToggleButton, the binding will lost.

问题是为什么切换按钮支持ICommand的,但不支持单向绑定?
我可以设置双向绑定,但它是坏主意时,切换按钮使用命令,因为实际操作中通过命令来处理,它不应该与双向复制结合,也有的时候是不可能的。在我的情况
命令= TogglePlayPause
=器isChecked {绑定到IsMediaPlaying}
IsMediaPlaying应该是只读的。

The question is why ToggleButton support ICommand but does not support OneWay binding? I can set TwoWay binding, but it is bad idea when ToggleButton use Command, because the actual operation handled by Command and it should not be duplicated with TwoWay binding, also some times it is not possible. in my case Command=TogglePlayPause IsChecked={Bind to IsMediaPlaying} IsMediaPlaying should be readonly.

所以,请告诉我如何使用命令使用切换按钮和约束其财产器isChecked?

So please tell me how use ToggleButton with Command and bind its IsChecked property?

推荐答案

您可以编写自己的 OneWayToggleButton 切换按钮并重写时,单击该按钮会发生什么:

You can write your own OneWayToggleButton by deriving from ToggleButton and override what happens when the button is clicked:

public class OneWayToggleButton : ToggleButton
{
    protected override void OnClick()
    {
        RaiseEvent(new RoutedEventArgs(ClickEvent, this));
        if (Command != null && Command.CanExecute(CommandParameter))
            Command.Execute(CommandParameter);
    }
}

和然后器isChecked 不会被按钮本身修改,并且仅可以通过结合来改变。但是因为我们仍然使用的切换按钮,控制的外观和行为通常是器isChecked 属性。

and then IsChecked won't be modified by the button itself and can only be changed via the binding. But because we are still using the IsChecked property of the ToggleButton, the control will look and behave normally.

这篇关于为什么不能做单向绑定到切换按钮物业器isChecked?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆