Thomas
Thomas

Reputation: 37

How to change ToggleButton IsPressed state?

I'd like to change the IsPressed state of a ToggleButton via C#. Unfortunately i get the message that says "The property or indexer 'ButtonBase.IsPressed' cannot be used in the context....".

Is there a way to set it via C# code?

Screen capture of example

Upvotes: 0

Views: 1565

Answers (2)

fussel
fussel

Reputation: 151

You can extend ToggleButton with this functionality and write your own ToggleButton.

public class PressableToggelButton : ToggleButton
{
    public new bool? IsChecked
    {
        get
        {
            return base.IsChecked;
        }
        set
        {
            base.IsChecked = value;

            if(value == true)
            {
                base.IsPressed = true;
            }
            else
            {
                base.IsPressed = false;
            }
        }
    }
}

Be careful, the new IsChecked hides the base property, which can lead to side effects. Please adapt this idea to your needs.

Upvotes: 1

Alexandre Asselin
Alexandre Asselin

Reputation: 195

The property you are looking for is "IsChecked". IsPressed is a property on ButtonBase which indicates that the left mouse button or SPACEBAR is pressed over the button.

this.MyToggleButton.IsChecked = true;

Upvotes: 2

Related Questions