Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获WPF文本框上的鼠标单击_C#_Wpf_Xaml_Textbox_Mouseevent - Fatal编程技术网

C# 捕获WPF文本框上的鼠标单击

C# 捕获WPF文本框上的鼠标单击,c#,wpf,xaml,textbox,mouseevent,C#,Wpf,Xaml,Textbox,Mouseevent,我想捕捉鼠标在文本框上的点击: <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">

我想捕捉鼠标在
文本框上的点击:

<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为 泡泡鼠标和鼠标向下 事件。因此,自定义事件 侦听MouseUp或 文本框中的鼠标向下事件将 不被称为如果您需要响应 对于这些事件,请倾听 隧道预演 改为PreviewMouseDown事件,或 将处理程序注册到 HandledEventsToo参数(后者 选项仅通过以下方式提供: 代码)。不要标记已处理的事件 除非你故意这么做 禁用文本框的本机处理 这些事件,请注意 对控件的 用户界面


在您的代码中,您只需启动MouseLeftButtonUp

就可以使用PreviewMouseDown事件,并在控制过程的内部部分单击之前以这种方式捕获任何单击:

<TextBox x:Name="t" PreviewMouseDown="TextBox_MouseDown" Height="32" Width="274" />

以下是使用MVVM的用户的代码示例

它适用于以下情况:

在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;
    }
<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>
视图中(xaml):

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;
    }
<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>


希望这能为您节省一些时间。

集中注意力对您来说至关重要吗?也许你可以禁用它?“文本框中的MouseUp或MouseDown事件将不会被调用”我认为真正的问题是,如果确实是这样,为什么这些函数会首先在api级别公开。