Let us say that you have created a System.Windows.Media.SolidColorBrush using the following line of code –
SolidColorBrush brush = new SolidColorBrush(Colors.Black);
And this brush is set as the background of the Window –
Background = brush;
Now, anytime you change brush.Color, the background will automatically change.
Wait a minute!! So if I change brush.Color, the background color of the window will change? That didn’t happen in GDI+. In GDI, you would have to painfully InvalidateRect and redraw the window with the new brush. Well I guess the same thing is happening behind the scenes, but it is a bit easier for you now. I never liked figuring out the rectangle that I needed to Invalidate anyway. :-)
The question is, who the heck is redrawing my rectangle? Obviously, it isn't me. :-)
The way this works is, back in the boonies, SolidColorBrush inherits from a class called System.Windows.Media.Freezable. Basically, a Freezable class is unfrozen to begin with. This means, an unfrozen class can be changed, and the runtime tracks the changes. As a change occurs, the framework will act upon it.
Here are a few other points –
- Freezable are unfrozen by default
- Unfrozen objects can be frozen by calling the .Freeze() method
- A frozen object is threadsafe, an unfrozen object is not.
- You cannot unfreeze an already frozen object, but you can make an unfrozen copy of it.
- The IsFrozen property will tell you if the object is frozen or not.
- Frozen objects perform better - so if you aren't going to change it, go ahead and freeze it.