1
votes

I have Grids on my listbox items and I want to choose their background color according the light or dark theme.

For exemple, if I'm in light theme the backgroud color of my grid will be red and if I'm in dark, the color will be blue. How can I do that in Xaml ?

..................
   <Grid Background="#3F0E0D0D"> //in this case is fix for both themes...
.................
    </grid>
...............
2

2 Answers

0
votes

You can't do it directly in XAML because this is hardcoded. Instead give to your Grid a name:

<Grid x:Name="MainGrid" Background="#3F0E0D0D"/>
...

and then in your code check the theme (like @radoslaf showed) and call:

if(isDark)
   MainGrid.Background=.... ; // whatever color is needed
else
   MainGrid.Background=.... ; // whatever color is needed
1
votes

You can get the theme on the phone in a very easy way.

For example, you can do something like:

 void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                //theme is dark
                //change grind background
            }
            else 
            {
                //theme is light
            }                
        }

Even better if you use OnNavigatedTo event or Application_Launchingand Application_Activated