c# - 捕获 WPF TextBox 上的鼠标点击

标签 c# wpf xaml textbox mouseevent

我想捕获鼠标在 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 获得键盘焦点后,我不再收到鼠标单击事件。知道如何让它工作吗?

最佳答案

TextBox Class

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

关于c# - 捕获 WPF TextBox 上的鼠标点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1874394/

相关文章:

c# - 在 WPF 中转换 PathGeometry

C#、事件处理程序和线程

c# - 存储 UI 设置的最佳实践?

c# - Entity Framework Core 和 SQL Server 2016 时态表

c# - 未能成功地将我的 ViewModel 数据公开给我的 XAML 绑定(bind) View

wpf - 从 WPF ListView 中的按钮传递值

c# - ASP.NET MVC5 Controller 操作只能使用 RedirectToAction 访问

c# - WPF MVVM : How to close a window

c# - XAML 绑定(bind) : binding to properties of a "global" object

c# - WPF缩放 Canvas 在鼠标位置上居中