单击WPF样式按钮后,它不会重置为默认值

时间:2011-05-05 12:29:35

标签: wpf button

我使用表达式混合和一半手工设置了一个按钮,它的效果相当不错,只要单击一个按钮,它就会保持“按下”状态,直到我点击另一个按钮。 MouseOver工作正常。有关如何修复的想法吗?

<Style x:Key="ButtonShrinkStyle" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="42" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="OnMouseEnter1">
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="glassCube">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="glassCube">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="outerRectangle">
                            <EasingPointKeyFrame KeyTime="0" Value="0,0"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.3" Value="0.5,0.5"/>
                        </PointAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="outerRectangle">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="outerRectangle">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="innerRectangle">
                            <EasingPointKeyFrame KeyTime="0" Value="0,0"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.3" Value="0.5,0.5"/>
                        </PointAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="innerRectangle">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="innerRectangle">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.9"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid>
                    <Rectangle x:Name="outerRectangle" Fill="Transparent" RadiusY="20" RadiusX="20" Stroke="GhostWhite" StrokeThickness="5">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <Rectangle x:Name="innerRectangle" Fill="{TemplateBinding Background}" Margin="12" RadiusY="20" RadiusX="20" Stroke="Transparent" StrokeThickness="5">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <ContentPresenter x:Name="myContentPresenter"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        <ContentPresenter.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </ContentPresenter.RenderTransform>
                    </ContentPresenter>
                    <Rectangle x:Name="glassCube" RadiusY="10" RadiusX="10" Stroke="{TemplateBinding Background}" StrokeThickness="5" Opacity="0" RenderTransformOrigin="0.5,0.5">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="White" Offset="0"/>
                                <GradientStop Color="Transparent" Offset="0.2"/>
                                <GradientStop Color="White" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="White" Offset="0.8"/>
                                <GradientStop Color="Transparent" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Mouse.MouseEnter">
                        <BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
                    </EventTrigger>
                    <Trigger Property="IsCancel" Value="False"/>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Opacity" TargetName="glassCube" Value="1"/>
                        <Setter Property="Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Stroke" TargetName="glassCube" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="False">
                        <Setter Property="Opacity" TargetName="glassCube" Value="0"/>
                        <Setter Property="Stroke" TargetName="outerRectangle" Value="GhostWhite"/>
                        <Setter Property="Stroke" TargetName="glassCube" Value="GhostWhite"/>
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" TargetName="glassCube" Value="1"/>
                        <Setter Property="Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Stroke" TargetName="glassCube" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Effect" TargetName="myContentPresenter">
                            <Setter.Value>
                                <BlurEffect  Radius="1"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Opacity" TargetName="glassCube" Value="0"/>
                        <Setter Property="Stroke" TargetName="outerRectangle" Value="GhostWhite"/>
                        <Setter Property="Stroke" TargetName="glassCube" Value="GhostWhite"/>
                        <Setter Property="Effect" TargetName="myContentPresenter">
                            <Setter.Value>
                                <BlurEffect  Radius="0"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Opacity" TargetName="glassCube" Value="0.7"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsPressed" Value="False" />
                            <Condition Property="IsMouseOver" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Opacity" TargetName="glassCube" Value="0"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:2)

当鼠标离开按钮时,MouseEnter事件中的动画不会反转。

创建一个动画,该动画将反转MouseEnter动画的动作并将其应用于MouseOut事件。

相关答案/示例:Here

答案 1 :(得分:0)

这是因为重点突出。如果单击按钮,它将获得焦点。所以你的重点触发器将起作用。如果将焦点或标签移动到其他控件,则可以。

 <Trigger Property="IsFocused"
                                 Value="True">
                            <Setter Property="Opacity"
                                    TargetName="glassCube"
                                    Value="1" />
                            <Setter Property="Stroke"
                                    TargetName="outerRectangle"
                                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                            <Setter Property="Stroke"
                                    TargetName="glassCube"
                                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        </Trigger>