在WPF中,为什么MouseLeave触发而不是MouseDown?

2024-03-25

这是我的代码:

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="stlNavButtonBorder" TargetType="Border">
            <Setter Property="BorderBrush" Value="#570000FF" />
            <Setter Property="BorderThickness" Value="5" />
            <Setter Property="Height" Value="100" />
            <Setter Property="Width" Value="200" />
            <Setter Property="Margin" Value="10" />

            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="BorderBrush.Color"
                                To="blue"
                                Duration="0:0:0.25"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="BorderBrush.Color"
                                To="#570000FF"
                                Duration="0:0:0.25" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseDown">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="BorderBrush.Color"
                                To="Black"
                                Duration="0:0:0.25" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle">
            <Setter Property="Fill" Value="#570000FF"/>
        </Style>

        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Style="{StaticResource stlNavButtonBorder}">
                            <Grid>
                                <Rectangle Style="{StaticResource stlNavButtonRectangle}"/>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <Button Content="Button 1" />
    <Button Content="Button 2"/>
    <Button Content="Button 3" />
</StackPanel>

它生成这些按钮:

When the mouse enters the button, it works as expected, and the border changes color: Blue border added to the picture when the mouse enters

问题是,当鼠标放在按钮上时,边框不会从蓝色变为黑色,正如我在MouseDown事件触发,而是消失,这是MouseLeave事件触发器。

如何修复它?谢谢。


我无法找到根本问题。如果我没记错的话,MouseDown事件被吞没Button for Click事件。无论如何,我希望下面的代码能够帮助您解决这个问题。

Update:

这是更新的代码,它将保留MouseLeave即使在之后触发IsPressed被触发。

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="stlNavButtonBorder" TargetType="Border">
            <Setter Property="BorderBrush" Value="#570000FF" />
            <Setter Property="BorderThickness" Value="5" />
            <Setter Property="Height" Value="100" />
            <Setter Property="Width" Value="200" />
            <Setter Property="Margin" Value="10" />

        </Style>
        <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle">
            <Setter Property="Fill" Value="#570000FF"/>
        </Style>

        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Style="{StaticResource stlNavButtonBorder}" x:Name="border">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.2"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" 
                                                                To="Black"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" 
                                                            To="Blue"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <Grid>
                                <Rectangle Style="{StaticResource stlNavButtonRectangle}"/>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>                         
                    </ControlTemplate>                        
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <Button Content="Button 1" />
    <Button Content="Button 2"/>
    <Button Content="Button 3" />
</StackPanel>

以下代码也适用,但单击按钮后的情况除外:鼠标输入后,当我们离开按钮时,它保持黑色.

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="stlNavButtonBorder" TargetType="Border">
            <Setter Property="BorderBrush" Value="#570000FF" />
            <Setter Property="BorderThickness" Value="5" />
            <Setter Property="Height" Value="100" />
            <Setter Property="Width" Value="200" />
            <Setter Property="Margin" Value="10" />

        </Style>
        <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle">
            <Setter Property="Fill" Value="#570000FF"/>
        </Style>

        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Style="{StaticResource stlNavButtonBorder}" x:Name="border">
                            <Grid>
                                <Rectangle Style="{StaticResource stlNavButtonRectangle}"/>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>                               
                            <Trigger Property="IsMouseOver" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="border"
                                                Storyboard.TargetProperty="BorderBrush.Color"
                                                To="Blue"
                                                Duration="0:0:0.25"/>               
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>

                                            <ColorAnimation Storyboard.TargetName="border"
                                                Storyboard.TargetProperty="BorderBrush.Color"
                                                To="#570000FF"
                                                Duration="0:0:0.25"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.ExitActions>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="border"
                                                Storyboard.TargetProperty="BorderBrush.Color"
                                                To="Black"
                                                Duration="0:0:0.25" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>                        
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <Button Content="Button 1" />
    <Button Content="Button 2"/>
    <Button Content="Button 3" />
</StackPanel>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在WPF中,为什么MouseLeave触发而不是MouseDown? 的相关文章

随机推荐

  • H2OGeneralizedLinearEstimator() - 预测误差

    我正在尝试使用 H2OGeneralizedLinearEstimator 函数预测 Kaggle comp 中的测试时间 模型在第 3 行正常训练 指标也都很合理 然而 当我进入预测步骤时 尽管测试数据帧与训练数据帧匹配 但我还是收到错误
  • 获取 for 循环中的下一个变量

    我对 Python 很陌生 我确信有一种更简单的方法来完成我需要的事情 但这里是 我正在尝试创建一个程序 对名为的字母列表执行频率分析inputList并检索 2 个字母对并将它们添加到另一个字典中 所以我需要它用所有 2 个字母对填充第二
  • 如何在list_display中显示内联元素?

    我有以下问题 我有两个模型 文章和评论 在评论中 我有parent models ForeignKey Article 我已将其设置为将 Comments 内联到 ArticleAdmin admin ModelAdmin 和 Commen
  • 在 WooCommerce 子类别存档页面上显示子子类别术语列表

    在 Woocommerce 中我使用获取 Woocommerce 档案中当前产品类别的子类别 https stackoverflow com questions 57767843 get the subcategories of the c
  • 使用基本身份验证从 wsdl url 生成 java 类

    我正在尝试从 WSDL 文件生成 java 类 该文件使用基本身份验证 虽然有很多插件 但我必须使用以下一个 org jvnet jaxb2 maven2 maven jaxb2 plugin With wsimport or wsdl2j
  • 从 QML 生成 KeyEvent

    如何生成 KeyEvent 我必须显示 Keys onPressed 的功能以及从虚拟键盘生成的事件 那么 当我的虚拟键盘事件生成时 我可以假装生成按键事件吗 我只能找到如何从 Qt 将KeyEvents 发送到 QML 但我想从 QML
  • http.sys 究竟是如何工作的[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试更深入地了解 IIS 的工作原理 我理解 http sys 是它的主要组件之一 然而 我一直很难找到有关它的易于理解的信息
  • Pygame水波纹效果

    我已经用 Google 搜索过它 但没有现成的脚本 与 Flash 上的相同效果相反 我已经检查过算法水效应解释 http www gamedev net page resources technical graphics programm
  • SuppressWarnings 不适用于 FindBugs

    我在 Eclipse 项目上运行 FindBugs 并收到一个潜在的错误警告 我想出于特定原因 在本问题的上下文之外 抑制该错误 这是代码 public class LogItem private String name private v
  • Visual Studio -- 不创建 exe 文件

    我正在使用 Visual Studio 2015 for C 并创建了以下基本程序 include
  • Bud1%@@@@E%DSDB`@@@是什么?

    我为客户制作了一个小应用程序 该应用程序扫描files包含几个文本文件的目录 然后它将每个文件读入一个字符串 每个文件都有标题和文章文本 这两部分用管道字符分隔 如下所示 article title article text 该脚本显示用于
  • 我自己的 Python OCR 程序

    我还是一个初学者 但我想写一个字符识别程序 这个程序还没有准备好 而且我编辑了很多 所以评论可能不完全一致 我将使用 8 个连通性来标记连通分量 from PIL import Image import numpy as np im Ima
  • 文件夹浏览器对话框的问题

    如果对话框中单击Make newfolder 则开始编辑刚刚创建的文件夹的名称并单击OK OKdialogrezalt返回 但在属性中SelectedPath他将文件夹命名为New文件夹 然后就有默认的名称 发生这种情况是因为当我们创建时
  • 为什么对 Deref::deref 结果断言会因类型不匹配而失败?

    以下是Deref示例来自Rust 编程语言 https doc rust lang org book first edition deref coercions html除了我添加了另一个断言 为什么assert eq与deref也相等 a
  • 如何在nodeJS项目中使用Jest全局Setup和Teardown?

    我使用 jest 将测试添加到我的 Node js 项目中 但对于每个测试套件 都有一个 beforeAll 方法用于创建新的测试服务器并连接到 mongo 数据库 还有一个 afterAll 方法用于关闭测试服务器和数据库 我想对所有测试
  • AWS DocumentDB 与 Robo 3T (Robomongo)

    我想将 Mac 笔记本电脑上的 Robo 3T 以前称为 robomongo 与 AWS 的 DocumentDB 连接 我遵循了大量教程 但找不到任何特定于 DocumentDB 的教程 在测试阶段 它通过了步骤 1 连接到我的 EC2
  • INSTALL_FAILED_OLDER_SDK 的 minSdkVersion 低于设备 API 版本

    在全新安装最新的 AndroidStudio 时 运行新项目模板 最小 SDK 选择为 15 ICS 尝试在运行 API 19 的 Nexus 5 上运行 我收到 INSTALL FAILED OLDER SDK 错误并显示以下输出 我没有
  • 类型不匹配:无法从连接转换为连接

    我想要 JDBC 连接到 MS Access 但 Class forName sun jdbc odbc JdbcOdbcDriver Connection con DriverManager getConnection jdbc odbc
  • 如何在 Room 中插入具有一对多关系的实体

    我正在使用 Room 构建一个数据库 但我不知道如何将具有关系 在我的例子中是一对多 的新元素插入到数据库中 没有解决方案曾经讨论过插入 他们只讨论了查询数据 这是 DAO Dao abstract class ShoppingListsD
  • 在WPF中,为什么MouseLeave触发而不是MouseDown?

    这是我的代码