views:

321

answers:

0

Hi everyone,

I have a ResourceDictionary, which consists of a Brush object and a Style using this Brush object for several animated properties in its Template property (via StaticResource markup extension). The problem is; when I merge the dictionary with the global application ResourceDictionary (Application.Resources) the Brush does not get freezed and every element sharing the Style gets affected by changes to the Brush.

Interestingly enough, when I move the Brush to a secondary merged ResourceDictionary, it gets freezed and everything works as expected (The freezable gets cloned before being animated) The problem happens only when a freezable object and some other resource referencing this object via the StaticResource markup extension reside in the same merged ResourceDictionary. I pasted the sample code for App.xaml, Window.xaml and Dictionary.xaml below. I would greatly appreciate if you could reproduce the same result and confirm that this is a bug in WPF.

NOTE: If you change the content type of the ResourceDictionary (Dictionary.xaml) from Page to Resource in Visual Studio (and thereof embed a XAML instead of BAML version of it into the compiled assembly) the problem disappears.

Window.xaml

<Window x:Class="WpfApplication1.Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300">
<StackPanel>
    <Button Height="30" Content="Test 1" Margin="5" />
    <Button Height="30" Content="Test 2" Margin="5" />
</StackPanel>

App.xaml

<Application x:Class="WpfApplication.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Dictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
<SolidColorBrush x:Key="backgroundBrush" Color="Aqua" />

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type Button}">
                 <Border Name="border" Background="{StaticResource backgroundBrush}">
                     <ContentPresenter />
                 </Border>

                 <ControlTemplate.Triggers>
                     <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                             <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Opacity" To="0" Duration="0:0:.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>

                         <Trigger.ExitActions>
                             <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Opacity" To="1" Duration="0:0:.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>