Skip to content

Can the state be changed in code? #9

Description

@sebastian-mg1

Hello, I was wondering if there is a way to change the state of the ToggleSwitch from code?

In most cases, when the user toggles the switch from one state to another (for example: From IsChecked=false to IsCheked=true), a process is launched to react to the toggle. In my case in specific, this process is asynchronous, therefore, if the process fails to complete, the ToggleSwitch should remain in the inital state before the toggle (for example: IsChecked=false). However, for UI/UX reasons, the desired experience is that the user toggles the state, which changes the ToggleSwitch state in the UI, and after the asynchronous process fails, from the code, call the ToggleSwitch to toggle its state again.

I tried simply changing the IsChecked property, but it is not working for me. Is this the proper way to toggle back the state?

toggleSwitch.IsChecked = false;
        <ts:HorizontalToggleSwitch x:Name="toggleSwitch" IsChecked="False" Margin="0,10" MinWidth="75"
                                   VerticalAlignment="Center" HorizontalAlignment="Center"
                                   Checked="HandleToggleSwitchIsChecked" Unchecked="HandleToggleSwitchIsUnchecked">
            <ts:HorizontalToggleSwitch.UncheckedContent>
                OFF
            </ts:HorizontalToggleSwitch.UncheckedContent>
            <ts:HorizontalToggleSwitch.CheckedContent>
                ON
            </ts:HorizontalToggleSwitch.CheckedContent>
        </ts:HorizontalToggleSwitch>

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions