MouseButtonEvent的Null异常 [英] Null exception at a MouseButtonEvent

查看:90
本文介绍了MouseButtonEvent的Null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在使用WPF c#应用程序.我有一个包含数据网格的用户控件.
我正在尝试通过右键单击从用户控件发送公共事件.
这是创建的公共事件

 公共 事件 MouseButtonEventHandler datagridMouseClick;



接下来,应该在datagrid的此事件处理程序上触发它:

 私有 无效 dataGrid1_MouseDown(对象发​​件人,MouseButtonEventArgs e)
{

    DependencyObject dep =(DependencyObject)e.OriginalSource;
    同时((dep!= )&&
    !(dep  DataGridCell)&&
    !(dep  DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    如果(dep  DataGridCell)
    {
        cell = dep  as  DataGridCell;
        同时((dep!= )&&!(dep  as  DataGridRow;

    }
  
         .datagridMouseClick(sender,e);
 
} 


OKK,所以这是我在一个具有数据网格的外部对象上存在的代码,我将将此用户控件加载到另一个项目WPF中,并将其命名为搜索",我在这里要做的是在事件处理程序datagrid1_MouseDown(这是datagrid鼠标单击事件的处理程序)内触发PUBLIC事件"datagridMouseClick".现在,我在这一点上触发公共事件"datagridMouseClick",以便能够从另一个项目(将包含该对象"search")中侦听此事件,这样我就可以从那里处理datagrid MouseClick事件(不在此对象内,而是在另一个对象内).
Other类初始化

 公共 Window1(){
InitializeComponent(); // 在此初始化对象搜索

            search.datagridMouseClick + =  MouseButtonEventHandler(search_datagridMouseClick); /*  这是我正在尝试听*/的事件

            search.datagridDoubleClick + =  RoutedEventHandler(search_datagridDoubleClick); /*  即使我使用与上述*/相同的方法,此事件仍可正常运行
} 



好的,所以我在这里不明白的是,从理论上讲datagridMouseClick绝不应该为null,因为在任何可能的单击之前(此为触发该事件的唯一方法),在此初始化点上已向处理程序订阅了处理程序.
因此,编译器永远不会进入

search_datagridMouseClick

内部,因为在触发时

datagridMouseClick

为null,
我希望我有点清楚,我知道我很想念这件事.但是我可以在这里经验丰富的人的帮助下使用.感谢您的耐心等待,请随时询问我是否不清楚.

解决方案

if (dep is DataGridCell)

不会检查空值

猜测,您的datagridMouseClick委托未设置-在第一行断点并单步检查.


这意味着没有其他类已注册接收该事件.您应该始终首先检查是否有其他注册了该事件的其他类,例如:-

 如果( this  .datagridMouseClick!=  .datagridMouseClick(sender,e);
            } 



希望对您有帮助


Hi there
I am using a WPF c# application. I have a user control which contains a datagrid.
I am trying to send a public event from the user control , on a right mouse click.
Here is the created public event

public event MouseButtonEventHandler datagridMouseClick;



Next its supposed to be fired on this event handler of the datagrid:

private void dataGrid1_MouseDown(object sender, MouseButtonEventArgs e)
{

    DependencyObject dep = (DependencyObject)e.OriginalSource;
    while ((dep != null) &&
    !(dep is DataGridCell) &&
    !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    if (dep is DataGridCell)
    {
        cell = dep as DataGridCell;
        while ((dep != null) && !(dep is DataGridRow))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
        row = dep as DataGridRow;

    }
  
        this.datagridMouseClick(sender, e);
 
}


OKK so this above is a code existing at an external object that i made, which has a datagrid, I will load this User Control into another project WPF and it will be named "search", the point of what i am doing here is to fire the PUBLIC event "datagridMouseClick" inside the eventhandler datagrid1_MouseDown (which is the handler of datagrid mouse click event) . Now i Fire the public event "datagridMouseClick" at this point in order to be able to listen to this event from the other project (which will contain this object "search") so that i will be able to handle the datagrid MouseClick event from there (not inside this object but from the other one).
The Other class initializes

public Window1(){
InitializeComponent();//Object search is initialized here

            search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick); /*this is the event i am tryin to listen to */

            search.datagridDoubleClick +=new RoutedEventHandler(search_datagridDoubleClick);/* this event works correctly eventhough i used the same method as the one above */
}



Ok so what i dont understand here is that theoratically datagridMouseClick should NEVER be null , since a handler is subscribed to it at this point of initialization before ANY possible click (which d be the only way to fire that event).
So,, the compiler never enters inside

search_datagridMouseClick

because the

datagridMouseClick

is null when fired,
I hope i was somewhat clear, i know it is something simple that i am missing. But i could use the help of someone more experienced here. Thank you for your patience, and feel free to ask if i was not clear.

解决方案

The line

if (dep is DataGridCell)

doesn''t check for null values


At a guess, your datagridMouseClick delegate is not set - pout a breakpoint on the first line and single step though to check.


It means that no other class has registered to receive the event. You should always check first whether there are any other classes that have registered to receive the event, like this:-

if (this.datagridMouseClick != null)
            {
                this.datagridMouseClick(sender, e);
            }



hope this helps


这篇关于MouseButtonEvent的Null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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