简体   繁体   English

尝试在 Clicked 事件后面的 Xamarin 代码中更改按钮的边框颜色

[英]Trying to change a button's border color in Xamarin code behind on a Clicked event

This just doesn't seem to be working, but of course, I feel like it really should be.这似乎不起作用,但当然,我觉得它确实应该如此。 Can't figure out what's wrong here.无法弄清楚这里出了什么问题。 I have a button with a border that's set to transparent in the xaml.我有一个在 xaml 中设置为透明边框的按钮。 I want the border color to change to white when the button is pressed.我希望按下按钮时边框颜色变为白色。 Very simple.很简单。 But yeah, any help would be appreciated.但是,是的,任何帮助将不胜感激。 Here's my event:这是我的活动:

private void FactionChosen(object sender, EventArgs e)
    {
        if ((sender as Button).BorderColor == Color.Transparent)
        {
            (sender as Button).BorderColor = Color.White;
        }
        else if ((sender as Button).BorderColor == Color.White)
        {
            (sender as Button).BorderColor = Color.Transparent;
        }
    }

Thanks in advance提前致谢

You should set a BorderWidth value.您应该设置一个 BorderWidth 值。

<Button Text="Click Me" Clicked="FactionChosen" BorderColor="Transparent" BorderWidth="5" />

在此处输入图像描述

As mentioned previously in my comment , two possible reasons could be the cause:正如我之前在评论中提到的,可能的原因有两个:

  1. You are not setting a value to BorderWidth , by default it has 0 as value您没有为BorderWidth设置值, 默认情况下它的值为 0

<Button Text="OK" BorderWidth="3" BorderColor="Transparent" Clicked="FactionChosen"/>

  1. Your Button is in a white background (the BackgroundColor of it Parent is white), it is straightforward that the BackgroundColor of the Parent control and the BorderColor of your Button must be different.您的Button处于白色背景(其 Parent 的BackgroundColor为白色),很简单,Parent 控件的BackgroundColor和您的 Button 的BorderColor必须不同。

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

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