gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:10:26 25 4
gpt4 key购买 nike

我想捕获鼠标在 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 thebubbling MouseUp and MouseDownevents. Consequently, custom eventhandlers that listen for MouseUp orMouseDown events from a TextBox willnot be called. If you need to respondto these events, listen for thetunneling PreviewMouseUp andPreviewMouseDown events instead, orregister the handlers with theHandledEventsToo argument (this latteroption is only available throughcode). Do not mark the event handledunless you deliberately want todisable TextBox native handling ofthese events, and be aware that thishas notable effects on the control'sUI.

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com