2

I seem to have run into some behavior regarding WPF ResourceDictionaries, Brushes, and Styles (at least that's what I've noticed so far) that is counter to my understanding of how these things should work. Basically, if I reference a Brush from a Setter withing a Style in a ResourceDictionary it causes the Brush to become frozen. The example below illustrates this, as I get an InvalidOperationException when I try to change the Color on the shared Brush within my button's Click event handler. It should cause both Rectangle's color to change, as they both use the same shared Brush, but I get the exception instead.

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TestBrush" Color="Red" />
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="{StaticResource TestBrush}" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Name="Button1" Content="Change Color" Click="Button1_Click" />
        <Rectangle Height="20" />
        <Rectangle Height="20" />
    </StackPanel>
</Window>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        var brush = (SolidColorBrush)FindResource("TestBrush");
        // InvalidOperationException Here.  Brush is Frozen/Read-Only
        brush.Color = Colors.Blue;
    }
}

If I simply remove the Style (more specifically the Setter) and reference the Brush (still from the ResourceDictionary) directly from each Rectangle, I get the expected behavior of the Rectangles' colors changing in tandem from the button click event. See code below (button click event hanlder remains the same).

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TestBrush" Color="Red" />
    </Window.Resources>
    <StackPanel>
        <Button Name="Button1" Content="Change Color" Click="Button1_Click" />
        <Rectangle Height="20" Fill="{StaticResource TestBrush}" />
        <Rectangle Height="20" Fill="{StaticResource TestBrush}" />
    </StackPanel>
</Window>

I only see the Brush becoming frozen when it is referenced as a StaticResource from a Style's Setter. I can actaully reference the same Brush from other locations within the ResourceDictionary without it becoming frozen; i.e. the contents of ControlTemplates.

Can anyone please explain what is going on with this strange behavior and if it's by-design or a bug?

Thanks, Brandon

H.B.
  • 166,899
  • 29
  • 327
  • 400
Brandon
  • 21
  • 3

1 Answers1

1

...once a style has been applied, it is sealed and cannot be changed. If you want to dynamically change a style that has already been applied, you must create a new style to replace the existing one. For more information, see the IsSealed property.

See http://msdn.microsoft.com/en-us/library/ms745683.aspx

Dmitry Polomoshnov
  • 5,106
  • 2
  • 24
  • 25
  • Thank you Dmitry, but I'm not trying to change the Style. My question refers to the brush becoming frozen (IsFrozen=true) simply by the presence of a Setter in the Style that refers to it as a StaticResource. Note that (although I mention this, but didn't include an example) I can actaully use the brush in other places within the Style without it becoming frozen so long as its not within a Setter (i.e. within the ControlTemplate itself). – Brandon Feb 13 '12 at 14:15
  • @Brandon well, yes, I see. WPF freezes style/template freezable values when sealed to improve performance and some threading issues. I do not know direct ways of how this can be prevented (or undone) so far. – Dmitry Polomoshnov Feb 14 '12 at 01:33