方法 : 読み取り専用の Freezable の書き込み可能なコピーを取得する

更新 : 2007 年 11 月

Clone メソッドを使用して読み取り専用の Freezable の書き込み可能なコピーを作成する方法を次の例に示します。

Freezable オブジェクトは、読み取り専用 ("固定") としてマークされた後には変更できません。ただし、Clone メソッドを使用して、固定されたオブジェクトの変更可能な複製を作成できます。

使用例

次の例では、固定された SolidColorBrush オブジェクトの変更可能な複製を作成します。

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;

Freezable オブジェクトの詳細については、「Freezable オブジェクトの概要」を参照してください。

参照

概念

Freezable オブジェクトの概要

参照

Freezable

CloneCurrentValue

その他の技術情報

基本要素に関する「方法」トピック