本当に厄介な問題に出くわしました。達成するのは簡単なことのように思えますが、解決策がわかりません。私がやりたいことは、ColorAnimation のToプロパティを MouseOverColor という DependencyProperty にバインドすることです。これは私のスタイルがどのように見えるかです:
<Style TargetType="{x:Type local:Button}">
<Setter Property="Background" Value="{StaticResource SolidBlueDark}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="MouseOverColor" Value="Cyan" />
<Setter Property="Padding" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.4" />
</VisualStateGroup.Transitions>
<VisualState Name="Normal" />
<VisualState Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" To="{TemplateBinding MouseOverColor}" />
</Storyboard>
</VisualState>
<VisualState Name="Pressed" />
<VisualState Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup Name="FocusStates">
<VisualState Name="Unfocused" />
<VisualState Name="Focused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border
Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Height="{TemplateBinding Height}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Width="{TemplateBinding Width}" />
<ContentPresenter
TextBlock.Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
何が起こるかというと、何も起こらず、ボタンの色は同じままです。ColorAnimation 行を次のように変更すると、色が変わります。
<ColorAnimation
Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).Color"
To="Cyan" />
しかし、To color を MouseOverColor プロパティで設定し、ハードコーディングしないようにする必要があります。どうすればこれを実現できますか? あらゆる種類のバインディングを試しましたが、結果は常に同じです。
前もって感謝します。
編集:静的リソースを使用しても機能しますが、それは私が望むものではありません。私がやりたいことをする方法は本当にありませんか?使用できるようにしたい「マウスオーバー」の色ごとに新しいスタイルを作成してハードコードするのは意味がありません。