IsPressed事件未在WPF中触发

时间:2014-02-14 14:30:40

标签: wpf xaml events button

我正在尝试编辑WPF应用程序按钮的默认模板,“MouseEnter”和“MouseLeave”事件正在运行,问题在于“IsPressed”事件基本上似乎没有被触发.I还删除了

RenderPressed="{TemplateBinding IsPressed}"

部分,但按钮似乎忽略了我的IsPressed事件 这是代码:

<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#F3F3F3" Offset="0"/>
    <GradientStop Color="#EBEBEB" Offset="0.5"/>
    <GradientStop Color="#DDDDDD" Offset="0.5"/>
    <GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="ButtonDark1" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Themes:ButtonChrome>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Control.MouseEnter">
                        <BeginStoryboard>
                            <Storyboard >
                                <DoubleAnimation Duration="0:0:0.3" To="0.8" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Control.MouseLeave">
                        <BeginStoryboard>
                            <Storyboard >
                                <DoubleAnimation Duration="0:0:0.3" To="0.5" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter Property="RenderDefaulted" TargetName="Chrome" Value="false"/>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Foreground" Value="LightBlue"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

提前致谢。

1 个答案:

答案 0 :(得分:1)

我们再次提出一个问题,即某人搞砸了他们的ControlTemplate并希望其他人修复它。答案总是一样的...... ControlTemplate 的默认Button开头。您可以在MSDN的Button Styles and Templates页面中找到ControlTemplate的默认Button

如果您查看链接页面,您会看到VisualStateManager.VisualStateGroups集合,在该集合中,您会找到名为VisualState的{​​{1}}。再进一步,你应该看到这个:

Pressed

这些是管理视觉<VisualTransition GeneratedDuration="0" To="Pressed" /> 状态的原因。

更改默认Pressed时,最好从整个默认XAML开始,然后首先使用自定义ControlTemplate。一旦你有了它,然后你可以开始一点一点地操纵它,并在每次改变后定期检查它。然后,如果它不起作用一次,你可以撤消最后几个更改并找出你做错了什么。