0

I set the background color of text box programmatically :

ColorBrush m_textBoxBackground = ColorBrush(Windows::UI::Colors::LightGray());

But now I have two themes Dark and Light. So I need to set the initial color from the theme.

How can I do it in c++ winrt? Any suggestion?

sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55
  • You could check the value of [Application.RequestedTheme property](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.application.requestedtheme?view=winrt-20348), then you could set the initial color of textbox according to this value. – dear_vv Jun 17 '21 at 05:26
  • 1
    As follows: `if (App.Current.RequestedTheme==ApplicationTheme.Light) {// set background} else { } }` – dear_vv Jun 17 '21 at 05:26
  • Has your question been resolved? If not, please feel free to contact us. – dear_vv Jun 23 '21 at 09:08
  • @AryaDing-MSFT As you mentioned worked as : if (App.Current.RequestedTheme==ApplicationTheme.Light) {// set background} else { } }. But I need to get the default color brush for a text box for a particular theme programatically. How can i do that? – sohel14_cse_ju Jun 23 '21 at 13:21
  • @AryaDing-MSFT the commencted section // set background. How do we get the default background color brush for the current theme and set it to a text box? – sohel14_cse_ju Jun 23 '21 at 13:24
  • When the application theme changes, the textbox background will change automatically, so you don’t need to set its background. When I set the RequestedTheme of this app to Light, the textbox background color is `#66FFFFFF`. When I set the RequestTheme to dark, the textbox background color is `#66000000`. – dear_vv Jun 24 '21 at 08:00
  • @AryaDing-MSFT I know about it. But In My scenario, I need to set the color programmatically. So I set the default color to a member variable : m_textBgColor = Windows::UI::Colors::LightGray(). And it's changed to other colors based on condition or stays to default. So I need to set the default color theme cause if a 3rd theme comes up I don't need to improve the code. So I need to set the color getting from the theme itself. Is there a way? – sohel14_cse_ju Jun 24 '21 at 10:14

1 Answers1

1

I have to say there is no such a Brush can change automatically according to the theme. You need to change the Brush of textbox manually. You could set a global SolidColorBrush type variable named myBrush, then use Color.FromArgb method to convert rgb color to Brush. As follows:

public SolidColorBrush myBrush;

if (App.Current.RequestedTheme==ApplicationTheme.Light) 
{ 
myBrush = new SolidColorBrush(Color.FromArgb(102,255,255,255));//#66FFFFFF
} 
else
{ 
myBrush = new SolidColorBrush(Color.FromArgb(102,0,0,0));//#66000000
}  
dear_vv
  • 2,350
  • 1
  • 4
  • 13
  • The above answer was helpful for another purpose. But not for my case. What do you think about this : https://stackoverflow.com/questions/58492188/how-to-reset-background-color-to-default-in-mvvm – sohel14_cse_ju Jun 27 '21 at 07:08
  • @sohel14_cse_ju what do you want indeed? A color brush that will automatically changed based on the theme change? So you don't need to set the color? – Ax1le Jun 29 '21 at 08:28
  • I have checked that answer, which uses data binding to reflect the change to UI. However, you also need to change the Brush in the code behind like the **Command** of that answer. – dear_vv Jun 29 '21 at 08:31