question

SinningMichael-4178 avatar image
1 Vote"
SinningMichael-4178 asked SinningMichael-4178 answered

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

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SinningMichael-4178 avatar image
1 Vote"
SinningMichael-4178 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.