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

时间:2015-07-28 09:03:42

标签: c# wpf xaml draggable drag

我试图在我的应用程序中移动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 永远不会触发任何建议。 我做错了什么或者有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

这可以防止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。