Drag and drop popup control in wpf

I need to drag the popup control to wpf and I was wondering if any of your guys could help me. I saw the following message:

Drag WPF Tooltip Control

but that's not how it should work ...? When I click and drag it, it always resets to a certain point, and moreover, commentators say this is not an effective approach ...? Does anyone have alternatives?

Thanks!

+4
source share
2 answers

You can open a child window with a custom border. Then add a MouseDown handler that allows you to drag and drop:

<Window WindowStyle="None" ShowInTaskbar="False" ResizeMode="NoResize" SizeToContent="Height" MouseDown="Window_MouseDown"> ... </Window> 

In the code behind:

  private void Window_MouseDown(Object sender, MouseButtonEventArgs e) { this.DragMove(); } 
0
source

We can write behavior to make any Popup draggable. Here is an example of a XAML popup associated with a text field that opens and stays open when the text field is focused:

 <Grid> <StackPanel> <TextBox x:Name="textBox1" Width="200" Height="20"/> </StackPanel> <Popup PlacementTarget="{Binding ElementName=textBox1}" IsOpen="{Binding IsKeyboardFocused, ElementName=textBox1, Mode=OneWay}"> <i:Interaction.Behaviors> <local:MouseDragPopupBehavior/> </i:Interaction.Behaviors> <TextBlock Background="White"> <TextBlock.Text>Sample Popup content.</TextBlock.Text> </TextBlock> </Popup> </Grid> 

Here is the behavior that allows us to drag Popup :

 public class MouseDragPopupBehavior : Behavior<Popup> { private bool mouseDown; private Point oldMousePosition; protected override void OnAttached() { AssociatedObject.MouseLeftButtonDown += (s, e) => { mouseDown = true; oldMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject)); AssociatedObject.Child.CaptureMouse(); }; AssociatedObject.MouseMove += (s, e) => { if (!mouseDown) return; var newMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject)); var offset = newMousePosition - oldMousePosition; oldMousePosition = newMousePosition; AssociatedObject.HorizontalOffset += offset.X; AssociatedObject.VerticalOffset += offset.Y; }; AssociatedObject.MouseLeftButtonUp += (s, e) => { mouseDown = false; AssociatedObject.Child.ReleaseMouseCapture(); }; } } 

If you are not familiar with the behavior, install the Expression Blend 4 SDK and add these namespaces:

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 

and add System.Windows.Interactivity to your project.

+15
source

Source: https://habr.com/ru/post/1336812/


All Articles