Alan2
Alan2

Reputation: 24572

How can I remove a color from a button in Xamarin?

In my C# I am changing to color of a button like this:

aButton.BackgroundColor = Color.FromHex("#e9e9e9");

Is there a way to remove this color. I know I could set it back to the color it was before but wondering if there is a way to just remove whatever was assigned?

Upvotes: 3

Views: 1890

Answers (2)

Dennis Schröer
Dennis Schröer

Reputation: 2412

According to the Xamarin documentation, the BackgroundColor of a VisualElement is Color.Default.

So you can just assign your Button.BackgroundColor property to Color.Default:

aButton.BackgroundColor = Color.Default;

You can't remove a color because it is a non-nullable value type. If you instead intend to make it transparent, assign it to Color.Transparent:

aButton.BackgroundColor = Color.Transparent;

Upvotes: 11

JKennedy
JKennedy

Reputation: 18799

You are changing the background colour of the Button. there is no way to "remove" the colour. I don't think this exists in any language (someone correct me if I'm wrong)

Your best bet is to reset the Colour:

public Color PreviousColour { get; set;}

public void SetColour()
{
    PreviousColour = aButton.BackgroundColor;
    aButton.BackgroundColor = Color.FromHex("#e9e9e9");
}

public void ResetColour()
{
    aButton.BackgroundColor = PreviousColour;
}

Upvotes: 1

Related Questions