Xamarin.forms: [Bug] IsEnabled does not work for Button

Created on 27 Jul 2019  ·  13Comments  ·  Source: xamarin/Xamarin.Forms

6772 # Description

I have set IsEnabled="false" to a button. But it doesn't disable the button.

Steps to Reproduce

  1. Create a Button
  2. add property IsEnabled
  3. set values ="false"

Expected Behavior

Disable the button and should not fire click events

Actual Behavior

Fires the click event

Basic Information

  • Version with issue: Xamarin.Forms 4.1
  • Last known good version: 3.5
  • IDE: Visual Studio 2017 Community/Enterprise
  • Platform Target Frameworks:

    • iOS: 12.3
    • Android: 9.0
  • Android Support Library Version: v7

  • Nuget Packages: Xamarin.Forms, Xamarin.Essentials
  • Affected Devices: All
needs-info ❓ needs-repro ❔ unverified bug

Most helpful comment

I understand how that can seem weird, but I think the explanation is pretty simple. It's all about the order in which the property values are applied. If you look at how the Command is implement, it just returns true for CanExecute whenever no condition is specified.

If the Command is assigned after the IsEnabled then it will override the IsEnabled value with true. If it is the other way around and IsEnabled is evaluated after you assign the Command the IsEnabled will be false. Good find though!

Please continue any further discussion in the linked issue above so it's easier to track this.

All 13 comments

Could you maybe create a small reproduction project that shows this problem? I’ve tried to create on myself but it seems to work in my case. Maybe we’re doing something different :)

I see you mentioned both iOS and Android? It did not disable on both?

Thanks!

Do you have a command bound on the button? I've seen isenabled behave strangely if the button has a bound command. I believe it will look at the CanExcecute property on the command to decide if its enabled or not.

Heres a repo:

<Button Text="test" BackgroundColor="Green" Command="{Binding Tapped}" CommandParameter="{Binding .}" IsEnabled="False" Margin="5,0,5,0">

the main bit being IsEnabled="False"

Thanks @JKennedy24 as mentioned in my former comment, I have tried to reproduce it and failed to do so. That is the reason to ask for a small reproduction project so I can determine what I have done wrong and/or what other factors might be of influence here.

@jfversluis I think the issue you might not be able to replicate it is that the command property needs a valid binding to it. I think @dwightbennett might be right in saying that the command.Canexecute property is interfering.

i.e. in my example make sure the button is bound to a viewmodel with a valid Tapped property in it.

If you then break the binding and rename it to {Binding Tappe} you see the IsEnabled property takes precedence.

Hope that helps

So then

the main bit being IsEnabled="False"

Is not the main bit but the binding is 😉 I'll retry it in a little bit

yep, sorry my bad. If you still can't replicate it give me a shout. I've also got my button in a popup page which may play a part. But I think it's just the button

I think I've managed to reproduce it. However, the initial issue states that it did work in 3.5. I've downgraded this project to the latest 3.5 and it shows the exact same behavior?

Repro6991.zip

I'm testing on latest stable (4.1.0.618606). I downloaded your project and upgraded to that version and can confirm thats the behaviour I'm seeing.

I will close this one, I see there has been an issue recorded for this already. Please follow the progress there.

Duplicate of #4754

So i do think there is a work around for this and its very odd. You can get the IsEnabled property to bind properly if you put it after the command binding in the xaml. Super weird right? I added a bindable bool to the project that @jfversluis added and created two buttons, one where the IsEnabled is the first property in the xaml and the one where its the last and when you run the project you'll see one button enabled and one button disabled. IsEnabled Example Project @kgbuddhima @JKennedy24

I understand how that can seem weird, but I think the explanation is pretty simple. It's all about the order in which the property values are applied. If you look at how the Command is implement, it just returns true for CanExecute whenever no condition is specified.

If the Command is assigned after the IsEnabled then it will override the IsEnabled value with true. If it is the other way around and IsEnabled is evaluated after you assign the Command the IsEnabled will be false. Good find though!

Please continue any further discussion in the linked issue above so it's easier to track this.

Was this page helpful?
0 / 5 - 0 ratings