Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can WPF objects deriving from Freezable be frozen in XAML?

Many types in WPF derive from Freezable. It provides immutability to mutable POCO objects and allows for improved performance in certain situations.

So my question is, how can I freeze objects in XAML markup?

(Note that I have posted a similar but different question too).

like image 717
Drew Noakes Avatar asked Apr 28 '09 21:04

Drew Noakes


People also ask

What is Freezable in WPF?

A Freezable is a special type of object that has two states: unfrozen and frozen. When unfrozen, a Freezable appears to behave like any other object. When frozen, a Freezable can no longer be modified. A Freezable provides a Changed event to notify observers of any modifications to the object.

Why XAML is used in WPF?

The goal of XAML is to enable visual designers to create user interface elements directly. WPF aims to make it possible to control all visual aspects of the user interface from mark-up.


1 Answers

To freeze a Freezable object declared in markup, you use the Freeze attribute defined in XML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation/options.

In the following example, a SolidColorBrush is declared as a page resource and frozen. It is then used to set the background of a button.

<Page    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   mc:Ignorable="po">    <Page.Resources>     <!-- This brush is frozen -->     <SolidColorBrush x:Key="MyBrush" po:Freeze="True" Color="Red" />   </Page.Resources>    <!-- Use the frozen brush -->   <Button Background="{StaticResource MyBrush}">Click Me</Button>  </Page> 

Source: Freezable Objects Overview

like image 136
CSharper Avatar answered Oct 02 '22 17:10

CSharper