#175 – Freeze Graphical Objects That You Don’t Intend to Modify

For performance purposes, it’s best to freeze graphical objects (e.g. brushes) if you don’t intend to modify them.  You must also freeze an object if you intend to reference it from a thread other than the thread that created it.

You can freeze an object in code, using the Freeze method.

            // SolidColorBrush, created in XAML, not frozen
            bool frozen = tealBrush.IsFrozen;    // frozen = false

            if (tealBrush.CanFreeze)

            frozen = tealBrush.IsFrozen;         // frozen = true

You can also freeze an object in XAML, when it is declared, with the addition of an XML namespace.

<Window x:Class="WpfApplication4.MainWindow"
        Title="MainWindow" Height="350" Width="525" >
        <SolidColorBrush x:Key="tealBrush" Color="Teal" po:Freeze="True"/>