我试图根据状态和事件为边框加上颜色。我有一个“打开”和“关闭”状态,并带有“ Poked”事件。开=绿色,关=红色,当拨开时,我想从蓝色逐渐变回其原始颜色(基于其状态)。所有这些都在应用程序资源中,并用作DynamicResource。

我有一些代码可以做到这一点,但是每当我触发Poked事件时,所有使用该对象当前设置的颜色的东西都会从Blue变为该颜色。我怀疑正在发生的是颜色转换实际上是在存储在应用程序资源中的颜色上运行的,而不是特定于对象的颜色。

我处理动画的方式可能不合时宜。感谢您的帮助。

MainWindow.xaml

<Window x:Class="ColorAnimatorIssue.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ColorAnimatorIssue"
    Title="MainWindow"
    Height="350"
    Width="525"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <local:StateColorConverter x:Key="ColorConverter"/>
    <Storyboard x:Key="Poke1">
        <ColorAnimation Storyboard.TargetName="B1"
                        Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)"
                        From="{DynamicResource PokeColor}"
                        Duration="0:0:2"/>
    </Storyboard>
    <Storyboard x:Key="Poke2">
        <ColorAnimation Storyboard.TargetName="B2"
                        Storyboard.TargetProperty="BorderBrush.Color"
                        From="{DynamicResource PokeColor}"
                        Duration="0:0:2"/>
    </Storyboard>
</Window.Resources>
<Grid>
    <Button Content="Poke 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="7,37,0,0" Click="Poke1_Click"/>
    <Button Content="Poke 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="87,37,0,0" Click="Poke2_Click"/>
    <Border x:Name="B1"
            BorderBrush="{Binding MyState1, Converter={StaticResource ColorConverter}}"
            BorderThickness="10"
            Margin="199,37,217,183">
        <Rectangle Height="100" Width="100" Fill="Black"/>
    </Border>
    <Border x:Name="B2"
            BorderBrush="{Binding MyState2, Converter={StaticResource ColorConverter}}"
            BorderThickness="10"
            Margin="305,37,111,183">
        <Rectangle Height="100" Width="100" Fill="Black"/>
    </Border>
    <Button Content="Toggle 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="7,10,0,0" Click="Toggle1_Click" />
    <Button Content="Toggle 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="87,10,0,0" Click="Toggle2_Click" />
</Grid>




背后的代码

public enum States
{
    On, Off
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{

    States myState1 = States.Off;
    States myState2 = States.Off;

    public MainWindow()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public States MyState1
    {
        get
        {
            return myState1;
        }
        set
        {
            if(myState1 != value)
            {
                myState1 = value;
                OnPropertyChanged("MyState1");
            }
        }
    }

    public States MyState2
    {
        get
        {
            return myState2;
        }
        set
        {
            if (myState2 != value)
            {
                myState2 = value;
                OnPropertyChanged("MyState2");
            }
        }
    }

    void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void Poke1_Click(object sender, RoutedEventArgs e)
    {
        (FindResource("Poke1") as Storyboard).Begin(this);
    }

    private void Poke2_Click(object sender, RoutedEventArgs e)
    {
        (FindResource("Poke2") as Storyboard).Begin(this);
    }

    private void Toggle1_Click(object sender, RoutedEventArgs e)
    {
        MyState1 = (MyState1 == States.On) ? States.Off : States.On;
    }

    private void Toggle2_Click(object sender, RoutedEventArgs e)
    {
        MyState2 = (MyState2 == States.On) ? States.Off : States.On;
    }
}


状态到颜色转换器

public class StateColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        switch((States)value)
        {
            case States.On:
                return Application.Current.FindResource("OnBrush") as Brush;
            case States.Off:
                return Application.Current.FindResource("OffBrush") as Brush;
            default:
                return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


应用资源

<Application.Resources>
    <Color  A="255" R="0" G="255" B="0" x:Key="OnColor"/>
    <SolidColorBrush Color="{DynamicResource OnColor}" x:Key="OnBrush"/>
    <Color  A="255" R="255" G="0" B="0" x:Key="OffColor"/>
    <SolidColorBrush Color="{DynamicResource OffColor}" x:Key="OffBrush"/>
    <Color  A="255" R="0" G="0" B="255" x:Key="PokeColor"/>
</Application.Resources>

最佳答案

尝试在您的x:Shared="False"资源上设置Brush,以强制每次引用该实例时都创建一个单独的实例。

10-08 18:01