Make WPF window draggable, no matter what element is clicked

前端 未结 9 1015
灰色年华
灰色年华 2020-12-04 07:47

My question is 2 fold, and I am hoping there are easier solutions to both provided by WPF rather than the standard solutions from WinForms (which Christophe

9条回答
  •  情深已故
    2020-12-04 08:15

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
    if (e.ChangedButton == MouseButton.Left)
        this.DragMove();
    }
    

    Is throwing an exception in some cases (i.e. if on the window you also have a clickable image that when clicked opens a message box. When you exit from message box you will get error) It is safer to use

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
    if (Mouse.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
    }
    

    So you are sure that left button is pressed at that moment.

提交回复
热议问题