WPF启用/禁用DragMove委托 [英] WPF Enable / Disable DragMove delegate

查看:61
本文介绍了WPF启用/禁用DragMove委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我得到了需要拖动的自定义窗口,但是在某些情况下,我需要忽略拖动.

目前,我在窗口构造函数中订阅委托

Hi all!

I got custom window which need to be dragged around but some cases I need to ignore dragging.

At the moment I subcribe delegate in window constructor

MouseLeftButtonDown += delegate { DragMove(); };



效果很好,使我可以在自定义窗口中拖动

当我想禁用拖动事件时,我打电话



Which works fine and enables me to drag my custom window around

When I like to disable drag event I call

MouseLeftButtonDown -= delegate { DragMove(); };



这不起作用,我仍然可以拖动我的自定义窗口..

那么我如何正确取消订阅我的代表?

希望你有主意:)

干杯!



Which does not work, Im still able to drag my custom window..

So how I properly unsubcribe my delegate?

Hope you got idea :)

Cheers!

推荐答案

您应该通过创建一个布尔变量(称为canDrag)作为表单级变量并显式创建MouseLeftButtonDown事件(仅在canDrag时才拖动)来做到这一点. =正确.像这样的东西:-

You should do this by creating a bool varaiable (call it canDrag) as a form level variable and explicitly creating a MouseLeftButtonDown event which will only drag if canDrag = true. Something like this:-

bool canDrag = false;

       public MainWindow()
       {
           InitializeComponent();
           MouseLeftButtonDown +=new System.Windows.Input.MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
       }

       void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
       {
           if (canDrag)
           {
               this.DragMove();
           }
       }



然后,您可以在适当的位置将canDrag设置为true和false.我使用按钮进行了如下测试:-



You can then set canDrag to true and false in the appropriate places. I used buttons to test like this:-

private void btnDrag_Click(object sender, RoutedEventArgs e)
        {
            canDrag = true;
        }

        private void btnNoDrag_Click(object sender, RoutedEventArgs e)
        {
            canDrag = false ;
        }



希望对您有帮助



Hope this helps


不起作用的原因是因为
The reason it doesn''t work is because
delegate { DragMove(); }




and

delegate { DragMove(); }


并且不相等,因此不会删除该委托.
您必须存储委托才能再次将其删除,例如:


and not equal thus the delegate is not removed.
You have to store the delegate in order to be able to remove it again, like this:

MouseButtonEventHandler handler = delegate { GetMoreData(); };
MouseLeftButtonDown += handler;
MouseLeftButtonDown -= handler;


这篇关于WPF启用/禁用DragMove委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆