Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Clone Method

Freezable.Clone Method

Creates a modifiable clone of this Freezable, making deep copies of this object's values. When copying this object's dependency properties, this method copies expressions (but they might no longer resolve) but not animations or their current values.

Namespace: System.Windows
Assembly: WindowsBase (in windowsbase.dll)

public Freezable Clone ()
public Freezable Clone ()
public function Clone () : Freezable
You cannot use methods in XAML.

Return Value

A modifiable clone of the current object. The cloned object's IsFrozen property will be false even if the source's IsFrozen property was true.

The Clone and CloneCurrentValue methods produce modifiable clones of frozen Freezable objects (they will also clone Freezable objects that are not frozen). The clone is effectively a deep copy of the current object.

The following table summarizes the differences between the Clone and CloneCurrentValue methods.

Action

Clone method behavior

CloneCurrentValue method behavior

Copying a dependency property that has an expression

The expression is copied, but might no longer resolve. See the Freezable Objects Overview for more information.

The current value of the expression is copied, but not the expression itself.

Copying an animated dependency property

The property's base (non-animated) value is copied. Animations are not copied.

The property's current animated value is copied. Animations are not copied.

Note that unset properties are not copied. If an unset property has a default value that is a frozen Freezable, that property value will remain frozen in the otherwise modifiable clone.

Move a Freezable Between Threads

This method can be useful for moving a Freezable between threads. First, make the Freezable unmodifiable by using its Freeze method. Now another thread can access the Freezable and make a local Clone that it can access.

Notes to Implementers: This method uses CloneCore to produce the clone. To modify the behavior of this method in a derived class, override the CloneCore method.

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.

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

// 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;

For more information about Freezable objects, see the Freezable Objects Overview.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2015 Microsoft