Drag WPF Popup control

前端 未结 6 1058
死守一世寂寞
死守一世寂寞 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:36

    Building off of Jobi Joy's answer, I found a re-useable solution that allows you to add as a control within xaml of an existing control/page. Which was not possible adding as Xaml with a Name since it has a different scope.

        [ContentProperty("Child")]
        [DefaultEvent("Opened")]
        [DefaultProperty("Child")]
        [Localizability(LocalizationCategory.None)]
        public class DraggablePopup : Popup
        {
            public DraggablePopup()
            {
                MouseDown += (sender, e) =>
                {
                    Thumb.RaiseEvent(e);
                };
    
                Thumb.DragDelta += (sender, e) =>
                {
                    HorizontalOffset += e.HorizontalChange;
                    VerticalOffset += e.VerticalChange;
                };
            }
    
            /// 
            /// The original child added via Xaml
            /// 
            public UIElement TrueChild { get; private set; }
    
            public Thumb Thumb { get; private set; } = new Thumb
            {
                Width = 0,
                Height = 0,
            };
    
            protected override void OnInitialized(EventArgs e)
            {
                base.OnInitialized(e);
    
                TrueChild = Child;
    
                var surrogateChild = new StackPanel();
    
                RemoveLogicalChild(TrueChild);
    
                surrogateChild.Children.Add(Thumb);
                surrogateChild.Children.Add(TrueChild);
    
                AddLogicalChild(surrogateChild);
                Child = surrogateChild;
            }
        }
    

提交回复
热议问题