WPF prism: using command on toggleswitch

391 Views Asked by At

I want to use a delegate command with a Horizontaltoggleswitch that is fired when the switch is toggled. I use delegate commands with button and also with textboxes (where the Event is fired when the enterkey is pressed), but I cant find how to solve that with toggleswitch? I tried this: XAML:

        <toggleSwitch:HorizontalToggleSwitch CheckedContent="Open" UncheckedContent="Closed" IsChecked="{Binding SubstrateValveOpen,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <toggleSwitch:HorizontalToggleSwitch.InputBindings>
                <KeyBinding Command="{Binding SubstrateCommand}" Key="??"/>
            </toggleSwitch:HorizontalToggleSwitch.InputBindings>
        </toggleSwitch:HorizontalToggleSwitch>

ViewModel:

        private bool _substrateValveOpen;
        public bool SubstrateValveOpen
        {
            get => _substrateValveOpen;
            set => SetProperty(ref _substrateValveOpen, value);
        }
        
         public DelegateCommand SubstrateCommand => new DelegateCommand(Substrate, CanSubstrate);

         ...
        

but I dont really know what to do with the Event. thanks for any idea!

1

There are 1 best solutions below

0
On BEST ANSWER

The command is completely superfluous here. The property should do everything here.

Reminder: a property ist not just a field with PropertyChanged...

private bool _substrateValveOpen;
public bool SubstrateValveOpen
{
    get => _substrateValveOpen;
    set
    {
        if (value && !_substrateValveOpen && !CanSubstrate())
            return;
        if (SetProperty(ref _substrateValveOpen, value) && value)
            Substrate();
    }
}