WPF: StaticResource animation updates other controls that use this resource

I have two TextBox that use the same StaticResource for their foreground color.

When I apply an animation that changes the color of the first TextBox, the color in the second TextBox also changes.

This does not happen if I do not use StaticResource, so I assume that the animation changes the color of the brush defined in the resource, and not the foreground color in the first text box.

Here is the code I'm using;

<Page.Resources>
    <SolidColorBrush x:Key="TextBrush"
                     Color="Black" />

    <Storyboard x:Key="Glow"
                TargetProperty="Foreground.Color"
                Storyboard.TargetName="txt1">
        <ColorAnimation To="Blue"
                        Duration="0:0:0.1" />
    </Storyboard>
    <Storyboard x:Key="Normal"
                TargetProperty="Foreground.Color"
                Storyboard.TargetName="txt1">
        <ColorAnimation To="Yellow"
                        Duration="0:0:0.1" />
    </Storyboard>
</Page.Resources>

<StackPanel>
    <StackPanel.Triggers>
        <EventTrigger RoutedEvent="StackPanel.MouseEnter">
            <BeginStoryboard Storyboard="{StaticResource Glow}" />
        </EventTrigger>
        <EventTrigger RoutedEvent="StackPanel.MouseLeave">
            <BeginStoryboard Storyboard="{StaticResource Normal}" />
        </EventTrigger>
    </StackPanel.Triggers>

    <TextBlock Name="txt1"
               Foreground="{StaticResource TextBrush}">Text One</TextBlock>
    <TextBlock Name="txt2"
               Foreground="{StaticResource TextBrush}">Text Two</TextBlock>
</StackPanel>

Anyway?

Matt

+3
source share
1 answer

StaticResource , Foreground . , , StaticResource .

, , StaticResource .

+3

Source: https://habr.com/ru/post/1789294/


All Articles