xamarin/Xamarin.Forms

Different behaviour is reproduced when changing the Opacity for normal Button and Custom Button

Open

#5,323 opened on Feb 20, 2019

 (1 comment) (0 reactions) (0 assignees)C# (1,926 forks)batch import
e/4 :clock4:help wantedinactivet/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Description

Opacity is not updated properly when using the Custom Button. Check the provided code snippet

Steps to Reproduce

1.Run the sample with the provided code snippet 2.Click the button 3.Again click the button

Expected Behavior

Opacity should be updated properly as like normal Button

Actual Behavior

Opacity doesn’t update properly

Code snippet

  <local:ExtendedButton x:Name="xamarinbtn" Text="Xamarin Button" Opacity="1" BackgroundColor="Red" />
<Button Text="Xamarin Button" x:Name="xamarinbtn2"  BackgroundColor="Red"/>

           

            <Button Text="change" Clicked="Handle_Clicked" /> 

void Handle_Clicked(object sender, System.EventArgs e)

        {
     xamarinbtn.IsEnabled = !xamarinbtn.IsEnabled;

            xamarinbtn2.IsEnabled = !xamarinbtn2.IsEnabled;

            if (xamarinbtn.IsEnabled)

            {

                xamarinbtn.Opacity = 1;

      xamarinbtn2.Opacity = 1;

            }

            else

            {

                xamarinbtn.Opacity = 0.5;

                xamarinbtn2.Opacity = 0.5;

            }
}

public class ExtendedButton : Button

    {

public Setter DisabledBackgroundSetter { get; set; }

  public ExtendedButton()

        { var visualStateGroupList = new VisualStateGroupList();

            var visualStateGroup = new VisualStateGroup();

            var disabledState = new VisualState();

            disabledState.Name = "Disabled";

            DisabledBackgroundSetter = new Setter()

            {

                Property = Button.BackgroundColorProperty,

                Value = Color.Transparent

            };

            disabledState.Setters.Add(DisabledBackgroundSetter);

            disabledState.Setters.Add(new Setter()

            {

                Property = Button.BorderColorProperty,

                Value = Color.Transparent

            });

            disabledState.Setters.Add(new Setter()

            {

                Property = Button.OpacityProperty,

                Value = 0.5

            });

 

            visualStateGroup.States.Add(disabledState);

 

            visualStateGroupList.Add(visualStateGroup);

 

            VisualStateManager.SetVisualStateGroups(this, visualStateGroupList);

 

            BorderWidth = 1;

            CornerRadius = 4;

            BackgroundColor = Color.Transparent;

   }

   }

Contributor guide