C# 在 WPF TextBox 上捕获鼠标点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1874394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture mouse clicks on WPF TextBox
提问by Robbert Dam
I want to capture mouse clicks on a TextBox
:
我想捕获鼠标点击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>
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
. 在TextBox
获得键盘焦点后,我不再收到鼠标单击事件。知道如何让这个工作吗?
采纳答案by Svetlozar Angelov
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.
TextBox 具有对冒泡 MouseUp 和 MouseDown 事件的内置处理。因此,不会调用从 TextBox 侦听 MouseUp 或 MouseDown 事件的自定义事件处理程序。如果您需要响应这些事件,请改为监听隧道 PreviewMouseUp 和 PreviewMouseDown 事件,或者使用 HandledEventsToo 参数注册处理程序(后一个选项只能通过代码获得)。不要将事件标记为已处理,除非您有意禁用 TextBox 对这些事件的本机处理,并注意这会对控件的 UI 产生显着影响。
In you code you are firing just MouseLeftButtonUp
在你的代码中,你只是在开火 MouseLeftButtonUp
回答by Nathan Wheeler
You can use the PreviewMouseDown event, and capture any clicks that way before the internal parts of the control process the click:
您可以使用 PreviewMouseDown 事件,并在控件的内部部分处理点击之前以这种方式捕获任何点击:
<TextBox x:Name="t" PreviewMouseDown="TextBox_MouseDown" Height="32" Width="274" />
回答by Matas Vaitkevicius
Here's code example for those who are using MVVM
这是使用 MVVM 的代码示例
It works fine for events that are inheriting from Control.
它适用于从 Control 继承的事件。
In ViewModel:
在视图模型中:
private ICommand _merchantRefereneceCommand;
public ICommand MerchantReferenceCopyToClipboard
{
get { return _merchantRefereneceCommand ?? (_merchantRefereneceCommand = new MerchantRefereneceCommand(this)); }
set { _merchantRefereneceCommand = value; }
}
public class MerchantRefereneceCommand : ICommand
{
private readonly PaymentViewModel _paymentViewModel;
public MerchantRefereneceCommand(PaymentViewModel paymentViewModel)
{
_paymentViewModel = paymentViewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
//Your code goes here.
}
public event EventHandler CanExecuteChanged;
}
In View (xaml):
在视图 (xaml) 中:
<TextBox Grid.Row="1" x:Name="MerchantReference" MaxLength="10" IsReadOnly="True"
Text="{Binding MerchantReference, Mode=OneWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick" >
<i:InvokeCommandAction Command="{Binding MerchantReferenceCopyToClipboard}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Hope this saves you some time.
希望这可以为您节省一些时间。