A draggable popup control in wpf

我的梦境 提交于 2019-12-18 17:26:26

问题


I need a draggable popup control in wpf and was wondering if any of your guys could help me out..I did see the following post:

Drag WPF Popup control

but that isnt how its supposed to work...? When i click and drag it always resets to a specific point and moreover the commenters said that this is not an efficient approach...? Does anyone have any alternatives?

Thanks!


回答1:


You could open a child Window with a custom border layout. Then add a MouseDown handler that enables the dragging:

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

In code behind:

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



回答2:


We can write a behavior to make any Popup draggable. Here is some sample XAML of a popup associated with a textbox that opens and stays open when the text box 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 the 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 behaviors, install the Expression Blend 4 SDK and add this namespaces:

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

and add System.Windows.Interactivity to your project.



来源:https://stackoverflow.com/questions/4784240/a-draggable-popup-control-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!