wpf中的可拖动弹出控件

发布于 2024-10-13 08:24:08 字数 242 浏览 2 评论 0原文

我需要在 wpf 中使用可拖动的弹出控件,想知道你们是否可以帮助我。我确实看到了以下帖子:

拖动 WPF 弹出控件

但这不是它应该如何工作...?当我单击并拖动时,它总是重置到特定点,而且评论者说这不是一种有效的方法......? 有人有其他选择吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-10-20 08:24:08

我们可以编写一个行为来使任何 Popup 可拖动。以下是与文本框关联的弹出窗口的一些示例 XAML,当文本框获得焦点时,该文本框将打开并保持打开状态:

<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>

以下是允许我们拖动弹出窗口的行为:

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();
        };
    }
}

如果您不熟悉行为,安装 Expression Blend 4 SDK 并添加此命名空间:

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

并将 System.Windows.Interactivity 添加到您的项目中。

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.

時窥 2024-10-20 08:24:08

您可以打开具有自定义边框布局的子窗口。然后添加一个支持拖动的 MouseDown 处理程序:

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

在后面的代码中:

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

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文