Freezable Objects in WPF

These are special type of objects that can have 2 states, one is modifiable (unfrozen) and other is read-only (frozen). When the object is in unfrozen state , it works like normal other objects. In the Frozen state, it is locked for any modification moreover in the Frozen state it can’t even fire events.

As you know now that the Freezable objects when unfrozen can be modifiable, so they should be monitored for any changes. Most of these objects are related to Graphics sub-system , and contain some unmanaged resources so even if you don’t modify it the system have to spend of its resources to monitor it.

It is suggested that making Freezable objects frozen can improve the performance of Application. Because now no resources spend for monitoring the object plus the frozen object can be now shared across threads unlike unfrozen which cannot be shared across threads.

Some Examples of freezable objects are brushes, pens, transformations, geometries, and animations.




Trying to Modify a Frozen Object

When the object is in ‘frozen’ state, it cannot be changed. So Suppose we have brush in frozen state. Now let us try to modify it and see what happens


SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myBrush.Freeze(); // Make the brush unmodifiable.
myBrush.Color = Colors.Red; // Trying to modify frozen brush

It throws InvalidOperationException. Shown Below

Freezable

What if we want to modify a Frozen object ?

We have to make a modifiable copy of the frozen object with the help of Clone method.

Now its enough theory Let us take an example.
We have taken a textbox and Brush whose default color is set to yellow. Then making that brush Frozen, makes it unmodifiable. So modifying a Frozen object, we need to clone it. Let us see how.

XAML Code

   <Grid x:Name="grd">
        <TextBox x:Name="txtName" Text="Hi Freezable Object" Height="50" Width="225" FontSize="25" />
    </Grid>

CS File

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Freezable();
    }
    private void Freezable()
    {
        SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
        if (myBrush.CanFreeze)
        {
            // Makes the brush unmodifiable.
            myBrush.Freeze();
        }

        txtName.Background = myBrush;

        if (myBrush.IsFrozen) // Evaluates to true.
        {
            // If the brush is frozen, create a clone and modify the clone.
            SolidColorBrush myBrushClone = myBrush.Clone();
            myBrushClone.Color = Colors.Red;
            txtName.Background = myBrushClone;
        }
        else
        {
            // If the brush is not frozen,
            // it can be modified directly.
            myBrush.Color = Colors.Red;
        }
    }
}

Output
Freezable

Freezing an Freezable Object through XAML

<Window x:Class="FreezableObjectWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FreezableDemo" Height="350" Width="305">
    <Window.Resources>
        <SolidColorBrush x:Key="MyBrushKey" po:Freeze="True" Color="Gold" />
    </Window.Resources>
    <Grid x:Name="grd">
        <TextBox x:Name="txtName" Background="{StaticResource MyBrushKey}" Text="Hi Freezable Object" Height="50" Width="225" FontSize="25" />
    </Grid>
</Window>