移动事件时未触发按钮单击

Posted

技术标签:

【中文标题】移动事件时未触发按钮单击【英文标题】:Button Click Not Fired on Move Event 【发布时间】:2015-10-18 17:51:35 【问题描述】:

我正在尝试在我的应用程序中移动 Button,它工作正常,但 ButtonClick 事件现在没有触发。

代码:

XAML

<Button Content="Button" Height="23" HorizontalAlignment="Left"
            Margin="190,108,0,0" Name="button1" VerticalAlignment="Top"
            Width="75" MouseLeftButtonUp="button1_MouseLeftButtonUp"
            MouseMove="button1_MouseMove" MouseUp="button1_MouseUp" Click="button1_Click"/>

C#

public MainWindow()
    
        InitializeComponent();
        button1.PreviewMouseUp += new MouseButtonEventHandler(button1_MouseUp);
        button1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler
                                              (button1_MouseLeftButtonUp);
        button1.PreviewMouseMove += new MouseEventHandler(button1_MouseMove);
    
double m_MouseX;
double m_MouseY;
public MainWindow()
    
        InitializeComponent();
        button1.PreviewMouseUp += new MouseButtonEventHandler(button1_MouseUp);
        button1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(button1_MouseLeftButtonUp);
        button1.PreviewMouseMove += new MouseEventHandler(button1_MouseMove);
    
private void button1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    
        // Get the Position of Window so that it will set margin from this window
        m_MouseX = e.GetPosition(this).X;
        m_MouseY = e.GetPosition(this).Y;
    

    private void button1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    
        if (e.LeftButton == MouseButtonState.Pressed)
        
            // Capture the mouse for border
            e.MouseDevice.Capture(button1);
            System.Windows.Thickness _margin = new System.Windows.Thickness();
            int _tempX = Convert.ToInt32(e.GetPosition(this).X);
            int _tempY = Convert.ToInt32(e.GetPosition(this).Y);
            _margin = mainGrd.Margin;
            // when While moving _tempX get greater than m_MouseX relative to usercontrol 
            if (m_MouseX > _tempX)
            
                // add the difference of both to Left
                _margin.Left += (_tempX - m_MouseX);
                // subtract the difference of both to Left
                _margin.Right -= (_tempX - m_MouseX);
            
            else
            
                _margin.Left -= (m_MouseX - _tempX);
                _margin.Right -= (_tempX - m_MouseX);
            
            if (m_MouseY > _tempY)
            
                _margin.Top += (_tempY - m_MouseY);
                _margin.Bottom -= (_tempY - m_MouseY);
            
            else
            
                _margin.Top -= (m_MouseY - _tempY);
                _margin.Bottom -= (_tempY - m_MouseY);
            
            mainGrd.Margin = _margin;
            m_MouseX = _tempX;
            m_MouseY = _tempY;
        
    

    private void button1_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    
        e.MouseDevice.Capture(null);

    

    private void button1_Click(object sender, RoutedEventArgs e)
    
        MessageBox.Show("HI");
    

button1_Click 永远不会触发,任何建议。 是我做错了什么还是有更好的方法?

【问题讨论】:

RoutedEventArgs e更改为EventArgs e @neiiic 不起作用 【参考方案1】:

这可以防止你的button1_Click 被触发:

e.MouseDevice.Capture(null);

我假设您希望消息框仅在您单击按钮而不拖动它时显示。

我会删除 MouseUp 事件并改为使用:

XAML

PreviewMouseDown="Button1_OnPreviewMouseDown"

C#

    double m_MouseX;
    double m_MouseY;
    double m_ClickedX;
    double m_ClickedY;

[...]

    private void Button1_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
    
        m_ClickedX = m_MouseX;
        m_ClickedY = m_MouseY;
    

    private void button1_Click(object sender, RoutedEventArgs e)
    
        if (m_ClickedX == m_MouseX && m_ClickedY == m_MouseY)
        
            MessageBox.Show("HI");
        
    

if 语句检查你是否移动了按钮,如果按钮没有被移动,则只显示 MessageBox。

【讨论】:

还是一样不触发 这很奇怪,我的发射顺利。这是我的完整代码:pastebin.com/wAknPqZV 忘记从构造函数中移除事件 我可以再抛出一个查询吗? 当然,我猜。与此有关吗?你已经在 SO 上问过了吗?

以上是关于移动事件时未触发按钮单击的主要内容,如果未能解决你的问题,请参考以下文章

Reactjs-onClick 事件在我第一次单击按钮时未触发

显示键盘时未在移动设备中触发提交事件

vb中单击命令按钮触发哪些事件

父元素阻止冒泡时未触发单击事件

MeteorJS:在弹出窗口中不会触发模板事件

鼠标退出按钮时未触发 MouseExited