WPF - How to change the offset of Popup

asked 2022-06-23T05:25:45.867+00:00
HoneyBee 146 Reputation points

I am dragging a Popup using WPF C#.

I have a 600*400 size control inside the popup.
So the Popup gets its size as is.

I made an event to change the offset of the popup by dragging the control and it works fine.

But when the popup moves to the edge of the screen
Pop-ups never leave the screen.

Because of the size of the popup.
Can't I just make the popup control draggable out of the screen area?

Click to vote
0 Votes"
0
HoneyBee-2519 answered • 2 secs ago
To briefly explain my situation,

inside my Popup control

I have a Grid control with 3 Columns.

The Border included in the 1st and 3rd columns of the Grid is,
Each time you click another button on the control, it will be shown or hidden.

Actually, only the part included in column 2 is always exposed.

In this case, if you drag
The popup area is set as much as the size of column 1 and column 3, so it does not move to the actual drag position on the screen.

Even though the content contained in columns 1 and 3 is hidden depending on the position of the popup,
I want to drag a popup based on column 2.
The size of the Popup control is
Is there any setting that allows it to leave the window area?

<Popup x:Name="InkToolPopup"   
               StaysOpen="True"  
               Placement="Center"  
               PopupAnimation="Fade"  
               Focusable="False"  
               AllowsTransparency="True">  
            <Control:InkTool x:Name="inkTool"  
                             MouseLeftButtonDown="inkTool_MouseLeftButtonDown"  
                             MouseMove="inkTool_MouseMove"  
                             MouseLeftButtonUp="inkTool_MouseLeftButtonUp"/>  
        </Popup>  
private void inkTool_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
        {  
            inkTool.CaptureMouse();  
            IsInkToolMouseDown = true;  
  
            InkToolMouseDownPos = InkToolPopup.PointToScreen(e.GetPosition(InkToolPopup));  
            InkToolLocationPos = InkToolPopup.PointToScreen(e.GetPosition(InkToolPopup));  
        }  
  
        private void inkTool_MouseMove(object sender, MouseEventArgs e)  
        {  
            if (IsInkToolMouseDown)  
            {  
                //var newMousePosition = e.GetPosition(this);  
                var newMousePosition = InkToolPopup.PointToScreen(e.GetPosition(InkToolPopup));  
                var offset = newMousePosition - InkToolLocationPos;  
  
                //if(!offset.Equals(new Vector(0, 0)))  
                if (offset.Length > 2)  
                {  
                    //oldMousePosition = newMousePosition;  
                    InkToolLocationPos = newMousePosition;  
                    InkToolPopup.HorizontalOffset += offset.X;  
                    InkToolPopup.VerticalOffset += offset.Y;  
                }  
            }  
        }  
  
        private void inkTool_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
        {  
            IsInkToolMouseDown = false;  
            inkTool.ReleaseMouseCapture();  
  
            if (VisualTreeHelper.HitTest(inkTool, e.GetPosition(inkTool)) != null)  
            {  
                Vector vector = InkToolMouseDownPos - InkToolPopup.PointToScreen(e.GetPosition(InkToolPopup));  
                if (vector.Length <= 2)  
                {  
                    var toggleButton = VisualTreeHelper.HitTest(inkTool, e.GetPosition(inkTool)).VisualHit.FindParent<System.Windows.Controls.Primitives.ToggleButton>();  
  
                    if (toggleButton != null)  
                    {  
                        switch(toggleButton.Name)  
                        {  
                            case "btn_InkToolControl":  
                                toggleButton.IsChecked = !toggleButton.IsChecked;  
                                break;  
                            case "btn_StrokePicker":  
                                toggleButton.IsChecked = !toggleButton.IsChecked;  
                                break;  
                                  
                            case "btn_Erase":  
                                inkTool.SetInkTool(true);  
                                break;  
                            default:  
                                if (!toggleButton.Name.Equals(beforeInkToolToggleButton))  
                                {  
                                    toggleButton.IsChecked = true;  
                                    beforeInkToolToggleButton = toggleButton.Name;  
                                }  
                                  
                                //inkTool.SetInkTool();  
                                inkTool.InkToolControl_PreviewMouseLeftButtonUp(toggleButton, e);  
                                break;  
                        }  
                    }  
                    else  
                    {  
                        var button = VisualTreeHelper.HitTest(inkTool, e.GetPosition(inkTool)).VisualHit.FindParent<Button>();  
                        if(button != null)  
                        {  
                            inkTool.InkToolButton_Click(button, e);  
                        }  
                    }  
                }  
            }  
        }  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,169 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
557 questions
{count} votes