我正在尝试创建一个简单的按钮样式,它将鼠标悬停时从0.0改为1.0的背景不透明度(反之亦然)。我正在为所述按钮创建一个模板,并且我绑定了模板中的所有属性。除了后台的SolidColorBrush
之外,它都能正常工作,我无法绑定到模板绑定。由于上下文我看到一些TemplateBinding
不是正确的,但我无法找到另一种解决方案。我怀疑,可能有Background
是Brush
的问题,我只需要该画笔的Color
组件,但我无法获得它。动画背景中的SolidColorBrush
显而易见的重写是使用两种不同的颜色(工作)创建两个模板样式,但我想避免这种硬编码和复制粘贴。我想要的是在Button上指定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"
属性设置进行测试,但没有奏效。
是的,但后来,有一个问题刷子的初始不透明度。我想避免需要指定alpha识别颜色。 –