Why could I not bind OneWay to the ToggleButton IsChecked property?

ToggleButton supports ICommand, so I create a lot of commands like TogglePlayPause, ToggleMute, and it works fine, but I need to bind the IsChecked property too, so that its checked state always displays the correct state. but when I create OneWay binding mode for ToggleButton and when I click ToggleButton, the binding will be lost.

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

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

+3
source share
1 answer

You can write your own OneWayToggleButtonby receiving ToggleButtonand overriding what happens when the button is pressed:

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

IsChecked . IsChecked ToggleButton, .

+3

Source: https://habr.com/ru/post/1789640/


All Articles