How can WPF objects deriving from Freezable be frozen in XAML?
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
Add this to your xaml namespace declarations:
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="po"
then, in your freezable objects, include this attribute
po:Freeze="True"