Issue
On default I have set my button color as Gainsboro, on button click, I set it to Orange. What I am having trouble is to revert the color back on the same button click event. How can I achieve this? I am trying to do a "on off switch" kind of button. Can I ask if this method is good? Because I also need to code for conditions if button is Orange or not
xaml
<Button x:Name="btnWIP" Text="non WIP" BackgroundColor="Gainsboro"/>
code
public bool isWIP = false;
private void btnWIP_Clicked(object sender, EventArgs e)
{
isWIP = true;
if(isWIP == true)
{
btnWIP.Text = "is WIP";
btnWIP.BackgroundColor = Color.FromHex("#FFBE57");
}
else
{
btnWIP.BackgroundColor = Color.Gainsboro;
}
}
Solution
You have a logic flaw in your code. You always set isWIP = true before checking it, and the condition will always be true.
You could change it up like this:
public bool isWIP = false;
private void btnWIP_Clicked(object sender, EventArgs e)
{
isWiP = !isWIP; // that way, you will change the value of isWIP on each click to true/false
if(isWIP == true)
{
btnWIP.Text = "is WIP";
btnWIP.BackgroundColor = Color.FromHex("#FFBE57");
}
else
{
btnWIP.BackgroundColor = Color.Gainsboro;
}
}
Answered By - Sotiris Koukios-Panopoulos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.