简体   繁体   中英

Storing the state of a ToggleSwitch Into A Variable in C# and XAML

EDIT : It's been solved. I slept on it and Bradley's comment helped me a ton! Thanks!

So I have a series of ToggleSwitches. Here's hows things are setup:

<ToggleSwitch
        x:Name="PrimaryToggle"
        Header="Primary"
        Toggled="PrimaryToggle_Toggled"/>

<ToggleSwitch
        x:Name="SecondaryToggle"
        Header="Secondary"
        Toggled="SecondaryToggle_Toggled"/>

<ToggleSwitch x:Name="Child1Toggle" Header="Child 1" />
<ToggleSwitch x:Name="Child2Toggle" Header="Child 2" />
<ToggleSwitch x:Name="Child3Toggle" Header="Child 3" />
<ToggleSwitch x:Name="Child4Toggle" Header="Child 4" />

In the code behind I have things setup like this:

// Parent Toggle (resets everything)
private void PrimaryToggle_Toggled(object sender, RoutedEventArgs e)
{
    if (PrimaryToggle.IsOn == true)
    {
        Child1Toggle.IsOn = false;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = true;

    }
    else if (PrimaryToggle.IsOn == false)
    {
        this.allToggles(false);
    }
}

/* This is where I need to store the current state of the child toggles
   so when it turns off it will restore the saved state of them */
private void SecondaryToggle_Toggled(object sender, RoutedEventArgs e)
{
if (SecondaryToggle.IsOn == true)
    {
        /* Need to store current state of children toggles
           In this section here: */
        Child1Toggle.IsOn = true;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = false;
    }

if (SecondaryToggle.IsOn == false && PrimaryToggle.IsOn == true)
    {
        /* Need to restore previous state of children 
       toggles in this section here: */
    }
}

So the problem is I'm pretty new to C# so I'm trying to track down the best way to handle this. Based on previous research I've come to the conclusion that I may need to use INotifyPropertyChanged and then set bindings on all the toggles I need to save, but I really do not know where to begin with that. I've done some { get; set; } stuff a while back but haven't used it in a while so I'm pretty much having a brainfart.

Any advice on where to begin?

Thanks!

So this solution isn't the most elegant, but it DOES work, so here we go.

You need to declare some bools in the class that can store the state of your ToggleSwitches:

public sealed partial class MainPage : Page
{
    public bool child1var { get; set; }
    public bool child2var { get; set; }
    public bool child3var { get; set; }
    public bool child4var { get; set; }

after you've set those, you'll need to set their data below

private void SecondaryToggle_Toggled(object sender, RoutedEventArgs e)
{
if (SecondaryToggle.IsOn == true)
    {
        // Here we save the state of the Child toggles 
        // before setting them to a different setting
        child1var = Child1Toggle.IsOn;
        child2var = Child2Toggle.IsOn;
        child3var = Child3Toggle.IsOn;
        child4var = Child4Toggle.IsOn;

        Child1Toggle.IsOn = true;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = false;
    }

Finally when we turn off the Secondary toggle, we set it back to what it was:

if (SecondaryToggle.IsOn == false && PrimaryToggle.IsOn == true)
    {
        Child1Toggle.IsOn = child1var;
        Child2Toggle.IsOn = child2var;
        Child3Toggle.IsOn = child3var;
        Child4Toggle.IsOn = child4var;
    }
}

This sets the data back to what it was before.

Hope this helps someone else! Again, I know this probably isn't the best solution, and there's probably a good way of sorting a lot of this stuff into other classes and such and if I learn those soon, I'll be sure to update this post with that information!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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