Drag WPF Popup control

前端 未结 6 1071
死守一世寂寞
死守一世寂寞 2020-12-13 14:32

the WPF Popup control is nice, but somewhat limited in my opinion. is there a way to \"drag\" a popup around when it is opened (like with the DragMove() method of windows)?<

6条回答
  •  余生分开走
    2020-12-13 15:14

    Here's a simple solution using a Thumb.

    • Subclass Popup in XAML and codebehind
    • Add a Thumb with width/height set to 0 (this could also be done in XAML)
    • Listen for MouseDown events on the Popup and raise the same event on the Thumb
    • Move popup on DragDelta

    XAML:

    
        
    
        
    
    

    C#:

    public partial class DraggablePopup : Popup 
    {
        public DraggablePopup()
        {
            var thumb = new Thumb
            {
                Width = 0,
                Height = 0,
            };
            ContentCanvas.Children.Add(thumb);
    
            MouseDown += (sender, e) =>
            {
                thumb.RaiseEvent(e);
            };
    
            thumb.DragDelta += (sender, e) =>
            {
                HorizontalOffset += e.HorizontalChange;
                VerticalOffset += e.VerticalChange;
            };
        }
    }
    

提交回复
热议问题