Raphael Teyssandier
Raphael Teyssandier

Reputation: 1777

ToggleButton is always true MVVM WPF

I have a little problem. I have 3 states for a togglebutton - two checked and one unchecked, but it is always checked. I don't know why.

XAML:

<ToggleButton Grid.Row="0" Grid.Column="1" Style="{DynamicResource MetroCircleToggleButtonStyle}" IsChecked="{Binding Path=RepeatChecked}" Command="{Binding Path=RepeatCommand}">
            <Image Source="../Ressources/repeat.png"></Image>
</ToggleButton>

C#:

private void RepeatFunction()
        {
            if (!this.RepeatChecked)
            {
                Console.WriteLine("Not checked");
                this.RepeatChecked = true;
                this.stateRepeat = StateRepeat.ONE;
            }
            else if (this.RepeatChecked && this.stateRepeat == StateRepeat.ONE)
            {
                Console.WriteLine("Not checked - 2");
                this.RepeatChecked = true;
                this.stateRepeat = StateRepeat.ALL;
            }
            else if (this.RepeatChecked)
            {
                Console.WriteLine("Checked");
                this.RepeatChecked = false;
                this.stateRepeat = StateRepeat.NONE;
            }
        }

The console write is always Checked. I really don't understand.

EDIT:

this.stateRepeat = StateRepear.NONE;
this.RepeatCommand = new CommandProvider(obj => RepeatFunction());

Upvotes: 0

Views: 1036

Answers (1)

user1672994
user1672994

Reputation: 10849

The problem is that on click of toggle button, you always set the RepeatChecked to false/true which is bind to IsChecked which updates toggle state sets unchecked/checked again; so checked changes to unchecked and unchecked changes to check. Comment the line in all three conditions and run the flow and you will see all conditions working.

private void RepeatFunction()
{
    if (!this.RepeatChecked)
    {
        Console.WriteLine("Not checked");
        ////this.RepeatChecked = true;
        this.stateRepeat = StateRepeat.ONE;
    }
    else if (this.RepeatChecked && this.stateRepeat == StateRepeat.ONE)
    {
        Console.WriteLine("Not checked - 2");
        ////this.RepeatChecked = true;
        this.stateRepeat = StateRepeat.ALL;
    }
    else if (this.RepeatChecked)
    {
        Console.WriteLine("Checked");
        ////this.RepeatChecked = false;
        this.stateRepeat = StateRepeat.NONE;
    }
}

Upvotes: 2

Related Questions