Xamarin.Forms Button ignores my BackgroundColor when binding to a command when canExecute changes

SigMilKSC 31 Reputation points
2021-03-28T11:32:56.327+00:00

I'm using a button in Xamarin.Forms with a bind to a command. The command has the property canExecute. When canExecute ist always true then the BackgroundColor works from XAML. When the canExecute becomes false then the BackgroundColor always changes between bluegrey and blue (yellow is ignored)

<Button x:Name="ScanButton" Text="Scan" BackgroundColor="Yellow" Command="{Binding ScanCmd}" />

With this codebehind the button is always yellow:

ScanCmd = new Command(
               execute: () => { var scanner = DependencyService.Get<IBarcodeScan>(); if (scanner != null) scanner.ScanBarcode(); },
               canExecute: () => true );

With this codebehind the button changes between bluegrey when canExute is false and blue when canExecute is true.

ScanCmd = new Command(
                   execute: () => { var scanner = DependencyService.Get<IBarcodeScan>(); if (scanner != null) scanner.ScanBarcode(); },
                   canExecute: () => aktFieldName != string.Empty );

When the button once was disabled then it loses his yellow background and becomes blue when it was enabled.

I'm using VS2019 and Xamarin.Forms 5.0.0.2012. In my older projects this was working.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} vote

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2021-03-29T02:57:47.287+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can bind a property to your Botton's BackgroundColor. I add a following property.

       Color _BTNbackground = Color.Red;  
               public Color BTNbackground  
               {  
                   get  
                   {  
                       return _BTNbackground;  
                   }  
         
                   set  
                   {  
                       if (_BTNbackground != value)  
                       {  
                           _BTNbackground = value;  
                           OnPropertyChanged("BTNbackground");  
         
                       }  
                   }  
         
               }  
    

    Then I add it to Button in the XAML.

       <Button Text="click" BackgroundColor="{Binding BTNbackground}" Command="{Binding TestCommand}"></Button>  
    

    When you need to change the background color of Button, you can set it directly.

       public bool IsEditing  
               {  
                   get  
                   {  
                       return _isEditing;  
                   }  
         
                   set  
                   {  
                       if (_isEditing != value)  
                       {  
                           if (_isEditing)  
                           {  
                               BTNbackground = Color.Yellow;  
                           }  
                           else  
                           {  
                               BTNbackground = Color.AliceBlue;  
                           }  
         
                           _isEditing = value;  
                           OnPropertyChanged("IsEditing");  
         
                       }  
                   }  
         
               }  
    

    I use a Button to change the value of IsEditing, if the IsEditing value is true. Button's background to Yellow, if the IsEditing value is false . Button's background to AliceBlue,

       private void Button_Clicked(object sender, EventArgs e)  
               {  
                    
                   IsEditing = !IsEditing;  
         
                  
               }  
    

    When canExecute true false, we can change the backgroundColor as normal. Here is running screenshot.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SigMilKSC 31 Reputation points
    2021-03-29T10:42:37.27+00:00

    Hello,

    thank you for your answer. I solved my problem with your suggestion. I set the "canExcute" of my command always to true and check in the "execute" of my command if this is allowed. The button is newer disabled, so it don't lose his Backgroundcolor. The user don't notice that the button is really not disabled. The color only suggests it to the user.

    Is this a feature or a bug, when a button is once diabled and then again enabled then it becomes always blue?
    Last year I developed an app for an older android 5, where this was working with the command. It seems to be a new "feature".

    Best Regards.
    Michael

    1 person found this answer helpful.
    0 comments No comments