Make WPF window draggable, no matter what element is clicked

前端 未结 9 995
灰色年华
灰色年华 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:29

    As already mentioned by @fjch1997 it's convenient to implement a behavior. Here it is, the core logic is the same as in the @loi.efy's answer:

    public class DragMoveBehavior : Behavior<Window>
    {
        protected override void OnAttached()
        {
            AssociatedObject.MouseMove += AssociatedObject_MouseMove;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
        }
    
        private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && sender is Window window)
            {
                // In maximum window state case, window will return normal state and
                // continue moving follow cursor
                if (window.WindowState == WindowState.Maximized)
                {
                    window.WindowState = WindowState.Normal;
    
                    // 3 or any where you want to set window location after
                    // return from maximum state
                    Application.Current.MainWindow.Top = 3;
                }
    
                window.DragMove();
            }
        }
    }
    

    Usage:

    <Window ...
            xmlns:h="clr-namespace:A.Namespace.Of.DragMoveBehavior"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
        <i:Interaction.Behaviors>
            <h:DragMoveBehavior />
        </i:Interaction.Behaviors>
        ...
    </Window>
    
    0 讨论(0)
  • 2020-12-04 08:32

    This is all needed!

    private void UiElement_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (this.WindowState == WindowState.Maximized) // In maximum window state case, window will return normal state and continue moving follow cursor
                {
                    this.WindowState = WindowState.Normal;
                    Application.Current.MainWindow.Top = 3;// 3 or any where you want to set window location affter return from maximum state
                }
                this.DragMove();
            }
        }
    
    0 讨论(0)
  • 2020-12-04 08:36

    Sure, apply the following MouseDown event of your Window

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

    This will allow users to drag the Window when they click/drag on any control, EXCEPT for controls which eat the MouseDown event (e.Handled = true)

    You can use PreviewMouseDown instead of MouseDown, but the drag event eats the Click event, so your window stops responding to left-mouse click events. If you REALLY wanted to be able to click and drag the form from any control, you could probably use PreviewMouseDown, start a timer to begin the drag operation, and cancel the operation if the MouseUp event fires within X milliseconds.

    0 讨论(0)
提交回复
热议问题