Syncfusion WPF FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

6. Concepts Freezable Object

FAQ Home
   6.1 How can I determine whether a Freezable Is Frozen?
   6.2 How can I obtain a writable copy of a Read-Only Freezable?



6.1 How can I determine whether a Freezable Is Frozen?


This example shows how to determine whether a Freezable object is frozen. If you try to modify a frozen Freezable object, it throws an 'Invalid Operation' Exception. To avoid throwing this exception, use the "IsFrozen" property of the Freezable object to determine whether it is frozen.

[C#]

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);

if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
myButton.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;
myButton.Background = myBrushClone;
}
else
{
// If the brush is not frozen,
// it can be modified directly.
myBrush.Color = Colors.Red;



6.2 How can I obtain a writable copy of a Read-Only Freezable?


This example shows how to use the Clone method to create a writable copy of a read-only Freezable. After a Freezable object is marked as read-only ("frozen"), you cannot modify it. However, you can use the 'Clone' method to create a modifiable clone of the frozen object.

The following example creates a modifiable clone of a frozen 'SolidColorBrush' object.

[C#]
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);


[C#]
// Freezing a Freezable before it provides
// performance improvements if you don't
// intend on modifying it.
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}

myButton.Background = myBrush;

// If you need to modify a frozen brush,
// the Clone method can be used to
// create a modifiable copy.
SolidColorBrush myBrushClone = myBrush.Clone();

// Changing myBrushClone does not change
// the color of myButton, because its
// background is still set by myBrush.
myBrushClone.Color = Colors.Red;

// Replacing myBrush with myBrushClone
// makes the button change to red.
myButton.Background = myBrushClone;


© 2001-2008 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap