WPF StaticResource vs DynamicResource

I have my Xaml as below

<Window.Resources>
    <SolidColorBrush x:Key="BrushMe" Color="Red"/>
    <SolidColorBrush x:Key="FreezeBrushMe" po:Freeze="true"  Color="Yellow"/>
</Window.Resources>
<StackPanel>        
    <Button Background="{StaticResource BrushMe}">Static</Button>
    <Button Background="{DynamicResource BrushMe}">Dynamic</Button>
    <Button Click="Button_Click">ChangeColorProperty</Button>
    <Button Click="Button1_Click">ChangeBrushObject</Button>
    <!--<Button Background="{StaticResource NotExistResource}" Click="Button_Click_1">GenerateResourceForStatic</Button>-->
    <Button Background="{DynamicResource NotExistResource}" Click="Button_Click_1">GenerateResourceForDynamic</Button>
    <Button Background="Green" Click="Button_Click_1">FixedBrush</Button>
    <Button Background="{StaticResource FreezeBrushMe}">FreezeBrushStatic</Button>
    <Button Background="{DynamicResource FreezeBrushMe}">FreezeBrushDynamic</Button>
    <Button Click="Button_Click_2">ChangeFreezeColorProperty</Button>
    <Button Click="Button_Click_3">ChangeFreezeBrushObject</Button>
</StackPanel>

In my code behind, it looks like

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SolidColorBrush brush = (SolidColorBrush)this.FindResource("BrushMe");
        brush.Color = Colors.Blue;
        this.Resources["BrushMe"] = brush;
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        SolidColorBrush newBrush = new SolidColorBrush(Colors.Yellow);
        this.Resources["BrushMe"] = newBrush;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.Resources.Add("NotExistResource", new SolidColorBrush(Colors.Green));
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        SolidColorBrush brush = (SolidColorBrush)this.FindResource("FreezeBrushMe");
        brush.Color = Colors.Blue;
        this.Resources["FreezeBrushMe"] = brush;
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        SolidColorBrush newBrush = new SolidColorBrush(Colors.Red);
        this.Resources["FreezeBrushMe"] = newBrush;
    }

My question is if I click ChangeBrushObject button first, it will change the Dynamic Button to Yellow, then I click ChangeColorProperty button, it will only change the Dynamic Button to Blue, and not affecting the Static button.

However, if I click the ChangeColorProperty button on its own, it will change both Static and Dynamic buttons to Blue.

Why it behaves like this? WPF doesn't throw any warnings when trying to assign a new object to a resource that has been referenced as static resource by the control?

I also tested the non-existing resource referenced by Dynamic Resource. As expected, if a StaticResource references a non-existing resource, Xaml will complain at compile time and DynamicResource does not. However, if I click GenerateResourceForDynamic button, it shows the green color as the background, but then it fades away, and reappears again, and repeats. Why it happens like this?

Edit: Added Freeze resource, strange behaviour again. If I click ChangeFreezeColorProperty button, it will through exception. And if I click ChangeFreezeBrushObject alone, it will change the FreezeBrushDynamic button color. However if I click ChangeFreezeBrushObject and then click ChangeFreezeColorProperty, no exception is thrown.

I think making a resource freeze will make the properties of the object linked with the resource to be readonly. that is why when changing the property, it will through the exception complaining about the readonly state. However freeze doesn't stop us from changing the object itself.

链接地址: http://www.djcxy.com/p/50426.html

上一篇: 在代码中设置WPF图像源

下一篇: WPF StaticResource vs DynamicResource