简体   繁体   English

按钮/活动时更改边框颜色 C# 其他按钮活动时设置回默认值

[英]Button / Change border color C# when active an set back to default when other button is active

I have three buttons.我有三个按钮。 Once I click on one it will change its border color to different color and show its active, but to set it default i have to click on the button again.一旦我单击一个,它会将其边框颜色更改为不同的颜色并显示其活动状态,但要将其设置为默认值,我必须再次单击该按钮。 All of these three buttons represent different action and they cant be running simutionasly for the user.所有这三个按钮都代表不同的动作,它们不能为用户模拟运行。 Do you know what to do to set back to default border color of button 1 if button 2 is clicked?如果单击按钮 2,您知道如何设置回按钮 1 的默认边框颜色吗?

Created my byttons in xamarin and this is the code behind.在 xamarin 中创建了我的字节,这是背后的代码。

I tried to create a separated method for that but i cant locate the buttons.我试图为此创建一个单独的方法,但我找不到按钮。

 protected void EnglishToCzech_Clicked(object sender, EventArgs e)
 {
     Button englishtoczech = (Button)sender;
     if (englishtoczech.BorderColor.Equals(Color.Default)) 
         english.BorderColor = Color.FromHex("#da2c43");
     else 
         englishtoczech.BorderColor = Color.Default;  
 }

 protected void CzechToEnglish_Clicked(object sender, EventArgs e)
 {
     Button czechtoenglish = (Button)sender;
     if (czechtoenglish.BorderColor.Equals(Color.Default)) 
         english.BorderColor = Color.FromHex("#da2c43");
     else
         czechtoenglish.BorderColor = Color.Default;
 }

 protected  void English_Clicked(object sender, EventArgs e)
 {
     Button english = (Button)sender;
     if (english.BorderColor.Equals(Color.Default))
         english.BorderColor = Color.FromHex("#da2c43");
     else
         english.BorderColor = Color.Default;
 }

Ok if I understand you correctly, you can reset the color of all in each click.好的,如果我理解正确,您可以在每次单击时重置所有颜色。 First you need to assign names to buttons.首先,您需要为按钮指定名称。 If you use XAML add x:Name attribute and give them names.如果您使用 XAML,请添加x:Name属性并为其命名。 I suppose you name them englishtoczech , czechtoenglish and english but you can name them anything.我想您将它们命名为englishtoczechczechtoenglishenglish但您可以将它们命名为任何名称。 The convention is camel case.公约是骆驼案。 Then you can use them in your code behind like this:然后你可以像这样在你的代码中使用它们:

 protected void EnglishToCzech_Clicked(object sender, EventArgs e)
 {
     if (englishtoczech.BorderColor.Equals(Color.Default))
     {
         englishtoczech.BorderColor = Color.FromHex("#da2c43");
         czechtoenglish.BorderColor = Color.Default;
         english.BorderColor = Color.Default;
     }
     else 
         englishtoczech.BorderColor = Color.Default;  
 }

 protected void CzechToEnglish_Clicked(object sender, EventArgs e)
 {
     if (czechtoenglish.BorderColor.Equals(Color.Default)) 
     {
         czechtoenglish.BorderColor = Color.FromHex("#da2c43");
         englishtoczech.BorderColor = Color.Default; 
         english.BorderColor = Color.Default;
     }
     else
         czechtoenglish.BorderColor = Color.Default;
 }

 protected void English_Clicked(object sender, EventArgs e)
 {
     if (english.BorderColor.Equals(Color.Default))
     {
         english.BorderColor = Color.FromHex("#da2c43");
         englishtoczech.BorderColor = Color.Default; 
         czechtoenglish.BorderColor = Color.Default;
     }
     else
         english.BorderColor = Color.Default;
 }

This is a pretty good simple approach but, two things you should consider:这是一个非常好的简单方法,但是,您应该考虑两件事:

  1. You shouldn't build your logic over button border colors and it is good to have some variable holding the state of system.你不应该在按钮边框颜色上构建你的逻辑,最好有一些变量来保存系统的状态。

  2. In more advanced scenarios if you are a professional I strongly recommend that you read MVVM and try that in a Xamarin project.在更高级的场景中,如果您是专业人士,我强烈建议您阅读 MVVM 并在 Xamarin 项目中尝试。 That makes your life a lot easier down the path这会让你的生活更轻松

Let me elaborate more on the first point.让我详细说明第一点。

You will set the border colors here but nothing happens.您将在此处设置边框颜色,但没有任何反应。 I think later on another event you will check the border color of buttons and do some logic with them.我想稍后在另一个事件中,您将检查按钮的边框颜色并对其进行一些逻辑处理。

This way you have built your logic over border colors.通过这种方式,您已经在边框颜色上构建了逻辑。 You can create an enum您可以创建一个enum

public enum State
{
    None,
    English,
    EnglishToCzech,
    CzechToEnglish
}

And then in your code bihind add a variable of that enum然后在您的代码中 bihind 添加该枚举的变量

private State state = State.None;

And finally set the state in each button click accordingly.最后在每个按钮点击中相应地设置状态。

This is just an example I hope it helps.这只是一个例子,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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