xamarin/Xamarin.Forms
Different behaviour is reproduced when changing the Opacity for normal Button and Custom Button
开放
#5,323 创建于 2019年2月20日
e/4 :clock4:help wantedinactivet/bug :bug:up-for-grabs
仓库指标
- 星标
- (5,644 个星标)
- PR 合并指标
- (30 天内没有已合并 PR)
描述
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;
}
}