単純なボタンスタイルを作成しようとしています。マウスオーバー時に背景の不透明度が0.0から1.0に変更されます(逆も同様です)。ボタンのテンプレートを作成していて、テンプレートのすべてのプロパティをバインドしています。バックグラウンドでSolidColorBrush
を除いてすべて正常に動作します。テンプレートバインドにバインドできません。私はTemplateBinding
のいくつかの言及が文脈のために正しいものではないことを見てきましたが、私は別の解決策を見つけることができません。私は、Background
がBrush
という問題があり、そのブラシのコンポーネントがColor
である必要がありますが、それを得ることはできません。背景でのSolidColorBrushのアニメーション
明白なオーバーライドは、2つの異なる色(いずれも機能する)で2つのテンプレートスタイルを作成することですが、そのようなハードコーディングとコピー貼り付けは避けたいと思います。私が望むものは、Background
というボタンのプロパティを指定するオプションです。これはSolidColorBrush
で使用され、残りの部分は不透明になります。
<Style TargetType="{x:Type Button}" x:Key="WindowButtonStyle">
<Setter Property="Width" Value="46" />
<Setter Property="Height" Value="32" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Border.Background>
<SolidColorBrush x:Name="ButtonBackgroundBrush" Color="???" Opacity="0.0" />
</Border.Background>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" />
</Border>
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOverAnimation">
<DoubleAnimation Storyboard.TargetName="ButtonBackgroundBrush" Storyboard.TargetProperty="Opacity" To="1.0" Duration="0:0:0.15" />
</Storyboard>
<Storyboard x:Key="MouseOutAnimation">
<DoubleAnimation Storyboard.TargetName="ButtonBackgroundBrush" Storyboard.TargetProperty="Opacity" To="0.0" Duration="0:0:0.15" />
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
すると、ボタンは次のように使用されます。
<Button x:Name="MinimizeButton" Style="{StaticResource WindowButtonStyle}" Click="MinimizeButton_Click" Background="Green">
<Image Source="../Resources/WindowButtons/Images/win-minimize.png" Width="12" Height="12"></Image>
</Button>
はそれをテストするために設定Background="Green"
プロパティを追加しましたが、働いていませんでした。
はい、が、その後、問題がありますブラシの初期不透明度を設定します。私は、アルファを意識した色を指定する必要性を避けたいと思います。 –