question

PatrickRyder avatar image
0 Votes"
PatrickRyder asked JarvanZhang-MSFT edited

Why can't I set Button BackgroundColor with Style?

I have an odd problem that has just developed, seemingly for no reason (i.e. I have not updated anything).

This works as expected:

 Content = new Grid()
 {
  BackgroundColor = Color.Transparent,
  RowDefinitions = Rows.Define( GridLength.Auto ),
  ColumnDefinitions = Columns.Define( 55, GridLength.Star, 55 ),
  Children =
  {
  new Button { Text = "Press Me", BackgroundColor = Color.Yellow }.Row( 0 ).Column( 1 )
  }
 };

This doesn't work. It did work but now has stopped since I last visited this code some time ago. As far as I can tell the button just gets the Xamarin default colour (light-blue)

 Style ButtonStyle = new Style( typeof(Button) )
 {
  Setters =
  {
  new Setter { Property = Button.BackgroundColorProperty, Value = Color.Yellow }
  }
 }
    
 Content = new Grid()
 {
  BackgroundColor = Color.Transparent,
  RowDefinitions = Rows.Define( GridLength.Auto ),
  ColumnDefinitions = Columns.Define( 55, GridLength.Star, 55 ),
  Children =
  {
  new Button { Text = "Press Me", Style = ButtonStyle, }.Row( 0 ).Column( 1 )
  }
 };

This code is so simple that I am struggling to think where I can look for an answer. I have never encountered a problem anything like this with Xamarin.

Does anyone have any idea what may be going on?

Kind wishes - Patrick

dotnet-xamarinforms
· 1
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.

If I attach an event handler to the button and check the Style in the debugger, it shows it should be yellow but it is displaying as light blue.

private void _button_Clicked( Object sender, EventArgs e )
{
Button b = (Button)sender;
Style s = b.Style;
}

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello PatrickRyder,​

Welcome to our Microsoft Q&A platform!

There are some errors when testing the above code. The 'RowDefinitions = Rows.Define' line code can not be complied correctly, do you use some other package. I created a basic demo to test the function, which works fine. Here is the sample code, you could refer to:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        var ButtonStyle = new Style(typeof(Button))
        {
            Setters =
            {
                new Setter { Property = Button.BackgroundColorProperty, Value = Color.Yellow }
            }
        };

        var grid = new Grid()
        {
            BackgroundColor = Color.Transparent,
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) }
            },
            ColumnDefinitions =
            {
                new ColumnDefinition()
            }
        };

        var button = new Button 
        { 
            Text = "Press Me", 
            Style = ButtonStyle 
        };

        grid.Children.Add(button);
        Grid.SetRow(button, 0);
        Grid.SetColumn(button, 0);

        Content = grid;

        button.Clicked += Button_Clicked;
    }
}

The style is consumed successfully on the button.

Best Regards,

Jarvan Zhang


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.


· 3
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.

Hi, @PatrickRyder
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·

@JarvanZhang-MSFT

I have hit another problem that is stopping me checking that. I'll get back to this thread as soon as I can.

Thank you.

  • Patrick

1 Vote 1 ·

Waiting for your update.

0 Votes 0 ·