捕获WPF TextBox上的鼠标单击 [英] Capture mouse clicks on WPF TextBox

查看:1223
本文介绍了捕获WPF TextBox上的鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获TextBox上的鼠标点击:

I want to capture mouse clicks on a TextBox:

<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox x:Name="t" MouseDown="TextBox_MouseDown"
                 MouseLeftButtonDown="TextBox_MouseLeftButtonDown"
                 MouseLeftButtonUp="TextBox_MouseLeftButtonUp"
                 Height="50" />
    </Grid>
</Window>

现在,我仅在用户首次输入TextBox时收到鼠标单击事件.此TextBox具有键盘焦点之后,我不再收到鼠标单击事件.知道如何使它正常工作吗?

Now I only receive a mouse click event when the user first enters the TextBox. After this TextBox has keyboard focus, I do not receive mouse click event anymore. Any idea how to get this working?

推荐答案

文本框类

TextBox具有内置的处理功能, 冒泡MouseUp和MouseDown 事件.因此,自定义事件 侦听MouseUp或 来自TextBox的MouseDown事件将 不被称为. 如果您需要回复 参加这些活动,听听 隧道化PreviewMouseUp和 而是使用PreviewMouseDown事件,或者 向处理程序注册处理程序 HandledEventsToo参数(后一个 该选项仅可通过 代码).不要将事件标记为已处理 除非您故意要 禁用TextBox的本机处理 这些事件,请注意 对控件的影响显着 用户界面.

TextBox has built-in handling for the bubbling MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a TextBox will not be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable TextBox native handling of these events, and be aware that this has notable effects on the control's UI.

在您的代码中,您仅触发了MouseLeftButtonUp

In you code you are firing just MouseLeftButtonUp

这篇关于捕获WPF TextBox上的鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆