【发布时间】:2012-02-06 19:11:02
【问题描述】:
我使用下面的 XAML 创建了一个复选框,但是由于某种原因,当鼠标移过控件时,我无法更改背景颜色。故事板用于设置不透明度,但背景颜色不会改变。知道我做错了什么吗?
<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}" />
<Setter Property="Foreground" Value="{StaticResource FGBrush}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="RenderOptions.EdgeMode" Value="Aliased"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator Background="Transparent" Height="20" MinHeight="{DynamicResource ResourceKey=MinimumIteractSizeDips}">
<BulletDecorator.Bullet>
<Grid MinHeight="{DynamicResource ResourceKey=MinimumIteractSizeDips}"
MinWidth="{DynamicResource ResourceKey=MinimumIteractSizeDips}"
Width="20"
Height="20"
SnapsToDevicePixels="True" RenderOptions.EdgeMode="Aliased" >
<Border x:Name="Border"
BitmapEffect="{StaticResource DropShadow}"
CornerRadius="0"
Background="{StaticResource ControlGradientBrush}"
BorderThickness="1"
BorderBrush="{StaticResource NormalBorderBrush}" RenderOptions.EdgeMode="Aliased"
MinHeight="{DynamicResource ResourceKey=MinimumIteractSizeDips}"
MinWidth="{DynamicResource ResourceKey=MinimumIteractSizeDips}"
Width="20"
Height="20" >
<Viewbox Stretch="UniformToFill" Margin="2" >
<Path
x:Name="CheckMark"
SnapsToDevicePixels="True"
Stroke="{StaticResource GlyphBrush}"
StrokeThickness="7"
RenderOptions.EdgeMode="Aliased"
Data="M 2 2 L 17 17 M 2 17 L 17 2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Viewbox>
</Border>
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter Margin="4,0,0,0"
VerticalAlignment="Top"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Trigger.EnterActions>
<BeginStoryboard Name="HighlightAnim">
<!--<Storyboard TargetName="Border" TargetProperty="Opacity" >
<DoubleAnimation To="0.5" Duration="00:00:00.1"/>
</Storyboard>-->
<Storyboard TargetName="Border">
<ColorAnimation Duration="0:0:0.15"
Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="Red" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="HighlightAnim"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
到底发生了什么,当我用你的代码尝试它时它可以工作,减去资源。
-
@Mark Hall 奇怪的是我的应用程序颜色没有改变,我在椭圆上尝试了完全相同的东西,也不会改变颜色。如果我取出情节提要并更改属性,它确实有效。
标签: wpf animation border storyboard