2016-01-22 7 views
12

Buttonの上にマウスがある場合、ButtonBackgroundをアニメートしたいと思います。UserControlアニメーションボタンの背景

ButtonさんBackground私は今私のUserControl

... Background="{Binding BGColor, Elementname="QButton"}" 

の背後にあるコードで作成したカスタム依存関係プロパティにバインドされて、私は

<Trigger Property="IsMouseOver" Value="True"> 
    <Trigger.EnterActions> 
     <BeginStoryboard> 
      <Storyboard> 
       <ColorAnimation To="LightBlue" 
           Duration="0:0:2" 
           Storyboard.TargetProperty="Background.Color"/> 
      </Storyboard> 
     </BeginStoryboard> 
    </Trigger.EnterActions> 
</Trigger> 
を使用してボタンの背景をアニメーション化しようとした場合

例外が発生しました:

can not anima不変のプロパティ(またはそれに類するもの)。

この問題を解決するにはどうすればよいですか? マイクHillbergの素晴らしい記事をもとに

+0

これらの[http://blogs.msdn.com/b/mikehillberg/archive/2006/09/26/cannotanimateimmutableobjectinstance.aspx](http://blogs.msdn.com/b/mikehillberg/archive /2006/09/26/cannotanimateimmutableobjectinstance.aspx)が役立つかもしれません[http://stackoverflow.com/questions/14383214/wpf-storyboard-in-style-returning-a-cannot-animate-color-on-an-immutable- obj](http://stackoverflow.com/questions/14383214/wpf-storyboard-in-style-returning-a-cannot-animate-color-on-an-immutable-obj) – spaceplane

答えて

3

についてCannot animate '...' on an immutable object instance

回避策として、あなたは Button用ブラシのコピーを作成するために結合更新することができます。これはバインディングを妨げません。 ウィンドウのフォアグラウンドへの変更はまだButtonに伝播されますが、 Buttonはローカルアニメーション用に独自のコピーを作成します。

だから、あなたのための完全なソリューションは、次のようにする必要があります:

それはこのようになります結合のため IValueConverterを参照している
<Window x:Class="WpfApplication2.Window3" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:local="clr-namespace:WpfApplication1" 
    .... 
    .... 

Background="{Binding BGColor, Converter={x:Static local:MyCloneConverter.Instance}}" 

class MyCloneConverter : IValueConverter 
{ 
    public static MyCloneConverter Instance = new MyCloneConverter(); 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is Freezable) 
     { 
      value = (value as Freezable).Clone(); 
     } 
     return value; 
    } 

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

変更DP(BGCOLOR)バックグラウンドを変更する

<Button.Triggers> 
    <EventTrigger RoutedEvent="MouseEnter"> 
     <BeginStoryboard> 
      <Storyboard> 
       <ColorAnimation To="Red" 
         Duration="0:0:2" 
         Storyboard.TargetName="QButton" 
         Storyboard.TargetProperty="(BGColor).(SolidColorBrush.Color)"/> 
      </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
</Button.Triggers>