question

HoneyBee-2519 avatar image
0 Votes"
HoneyBee-2519 asked HoneyBee-2519 edited

WPF - How to change the offset of Popup

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-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@HoneyBee-2519. Welcome Microsoft Q&A. Did you add other Nuget package references to your project? Is your Control:InkTool custom? What is the complete code for Control:InkTool ? Or is it possible to show a minimal project that reproduces the problem?

0 Votes 0 ·

@HuiLiu-MSFT
no. It's a simple CustomUserControl I made.




214296-inktoolxaml.txt


0 Votes 0 ·
inktoolxamlcs.txt (20.4 KiB)
inktoolxaml.txt (72.8 KiB)

0 Answers