Dynamically Change Grid Background Color/Image in Windows Phone 8/8.1 App

To change the background color/image of the Grid background in Windows Phone 8 or Windows Phone 8.1 you can use the following procedure. Well I am not sure this works for Windows 10 for Mobile also! If you have set same background image or color for the entire app then you want to change the color or image that is coming from cms or a cloud, then you are in the right place.

Step 1

Verify this you have set your Style like this in all pages or in the required page of your app like this.

set your Style

Step 2

Go to the app.xaml the Page then create an empty Setter Property with Key=LayoutGridStyle.

LayoutGridStyle
Here you can Create a Static Style Also As Mentioned Below,Which Works as a Default Image.If there is no image coming from cms or cloud.
You should not use a Name instead of a Key. This won't work out here "Because x:Key is only valid inside a resource dictionary and is added to a dictionary, x:Name is used locally and represents a variable within the class.".

If you are giving the Setter property in the same page where you set your style there you can use the x:Name, but here we are adding to the dictionary of the app resource so you should use x:Key.

Step 3

Now to change the color, just use the following code.

  1. System.Windows.Style style = new System.Windows.Style(typeof(Grid));  
  2. style.Setters.Add(new Setter(Grid.BackgroundProperty, new SolidColorBrush(Utility.ConvertStringToColor(Global.BackGroundColor))));  
  3. if (!ReferenceEquals(Application.Current.Resources.FirstOrDefault(rr = > rr.Key.ToString() == "LayoutGridStyle"), null)) {  
  4.     Application.Current.Resources.Remove("LayoutGridStyle");  
  5. }  
  6. Application.Current.Resources.Add("LayoutGridStyle", style);  
  7. (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/View/Menu/MainMenu.xaml", UriKind.Relative));  
Here Global.BackGroundColor is a string that contains a Color Code like "#00FFFF", if you want to use a Hexa Color Code then you need a Converter like this. Create a new class file and save it in the Utility Folder and just refer to the namespace.
  1. public static Color ConvertStringToColor(String hex) {  
  2.     //remove the # at the front  
  3.     hex = hex.Replace("#""");  
  4.   
  5.     byte a = 255;  
  6.     byte r = 255;  
  7.     byte g = 255;  
  8.     byte b = 255;  
  9.   
  10.     int start = 0;  
  11.   
  12.     //handle ARGB strings (8 characters long)  
  13.     if (hex.Length == 8) {  
  14.         a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);  
  15.         start = 2;  
  16.     }  
  17.   
  18.     //convert RGB characters to bytes  
  19.     r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);  
  20.     g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);  
  21.     b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);  
  22.   
  23.     return Color.FromArgb(a, r, g, b);  
  24. }  
  25.   
If you want, use a normal color, just change: 
  1. style.Setters.Add(new Setter(Grid.BackgroundProperty, new SolidColorBrush(Colors.Orange)));   
For setting the image as background use the following step.

Statically
  1. <Application.Resources>  
  2.     <Style x:Key="LayoutGridStyle" TargetType="Grid">  
  3.         <Setter Property="Background">  
  4.             <Setter.Value>  
  5.                 <ImageBrush ImageSource="/Assets/Squash.jpg"/>  
  6.             </Setter.Value>  
  7.         </Setter>  
  8.     </Style>  
  9. </Application.Resources>  
Dynamically
  1. ImageBrush myBrush = new ImageBrush(); 
  2. myBrush.ImageSource = new BitmapImage(new Uri("Assets/Squash.jpg", UriKind.Relative));
  3. LayoutGridStyle.Setters.Add(new Setter(Grid.BackgroundProperty, myBrush));
    if (!ReferenceEquals(Application.Current.Resources.FirstOrDefault(rr => rr.Key.ToString() == "LayoutGridStyle"), null))
    {
    Application.Current.Resources.Remove("LayoutGridStyle");
    }
    Application.Current.Resources.Add("LayoutGridStyle", LayoutGridStyle);

  4. (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/View/Menu/MainMenu.xaml", UriKind.Relative));  
Or:
  1. RootFrame = new PhoneApplicationFrame {  
  2.     Background = new ImageBrush() {  
  3.         ImageSource = new BitmapImage(new Uri("Assets/Squash.png", UriKind.Relative)),  
  4.         Stretch = System.Windows.Media.Stretch.None,  
  5.         AlignmentX = AlignmentX.Center,  
  6.         AlignmentY = AlignmentY.Center  
  7.     }  
  8. };  
If you have any doubts then please comment below or if you know a better way then this please comment below, Thank You!

Up Next
    Ebook Download
    View all
    Learn
    View all