locked
Grid Background in WPF RRS feed

  • Question

  •  

    Hi All,

     

    I want to change the Background color for the Grid since the Background Property Expects the Brushes..i want to customise the grid so that i can set the color using RGB instead of using the Predefined Colors like Transparent,Alice ,Bisque and etc........

     

    Kindly Provide the Solution in C#.

     

     

    Regards

    S.SARAVANAN

    Thursday, August 9, 2007 9:23 AM

Answers

  • Hi,

    It's myGrid.Background = new SolidColorBrush(Color.FromArgb(50, 100, 150,100));

    Best regards,

    Benny
    Thursday, August 9, 2007 3:10 PM

All replies

  • For the Background in WPF you can also use a hexadecimal value:

    <Grid Background="#B5CBEF">

    Thursday, August 9, 2007 10:48 AM
  • Thanks For ur Reply,

     

    But i want to apply the RGB Color in C# Code Behind,Not in the XAML.

     

     

     

    Thursday, August 9, 2007 2:04 PM
  • Hi, try the following

     

    Code Snippet

     

    myGrid.Background = new SolidBrush(Color.FromArgb(50, 100, 150));

     

     

     

    Keep in mind that Background is a Brush, so you can use other brushes such as gradients, textures and so on.

     

    Luc

     

    Thursday, August 9, 2007 2:26 PM
  • It Doesnt worked out ..

    Getting the Error as Cannot Implictly Convert the SolidBrush to MediaBrush

     

    Thursday, August 9, 2007 2:55 PM
  • Hi,

    It's myGrid.Background = new SolidColorBrush(Color.FromArgb(50, 100, 150,100));

    Best regards,

    Benny
    Thursday, August 9, 2007 3:10 PM
  • Hi ,

    The Code is Getting the Exception as Objects Reference not set to an instance of.

    actually two Color Class is available in .net

    1) System.Drawing.Color

    2) System.Windows.Media.Color

     

    WPF is not accepting the System.Drawing.Color it is accepting the System.Windows.Media.Color but getting the exception.

    What 2 do?

     

    Following is my code.

     

    LeftColumnGrid.Background =new SolidColorBrush(System.Windows.Media.Color.FromArgb(50, 100, 150, 100));

     

    Give Solution

    Friday, August 10, 2007 6:25 AM
  • Well, your code works fine on my machine. Are you sure that LeftColumnGrid is instantiated at the time you try to set the Background?
    • Proposed as answer by AleksMA Sunday, November 25, 2012 3:34 AM
    Friday, August 10, 2007 6:29 AM
  • Yes i did a mistake in calling the Code. what i did is i had called the code b4 intialisecomponent() thats the problem occurs. now its fine .thank 4 the Repy.Sorry 4 the mistake i had done.Happy .NET ing

     

    Friday, August 10, 2007 7:00 AM