How to check for the current background color of a grid?
I'm trying to basically make a toggle button to change the background color of a Windows Phone 7 app.
I'm changing the background color of the Grid named LayoutRoot using this code:
LayoutRoot.Background = new SolidColorBrush(Colors.White);
After I've done that, I want to check the value of LayoutRoot.Background
in an if statement (to serve as the toggle). This is where I'm running into problems. I can't seem to come up with a way to check that value.
When I do a LayoutRoot.Background.ToString()
, I get System.Windows.Media.SolidBrushColor
back as the value. I suppose this makes sense, since the background is a SolidBrushColor. But how do 开发者_开发技巧I get access to the value, so I can check it in my if statement?
You can do:
SolidColorBrush brush = LayoutRoot.Background as SolidColorBrush;
if (brush != null) {
if (brush.Color == Colors.White) {
// Do something
}
}
Other possible brushes include LinearGradientBrush and RadialGradientBrush, so SolidColorBrush is just one of many possible brush types. Which is why there is a if-statement checking for null.
精彩评论