在另一个类中访问MouseLeftButtonUp EventArgs [英] Access MouseLeftButtonUp EventArgs in another Class

查看:66
本文介绍了在另一个类中访问MouseLeftButtonUp EventArgs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



i有两个班级,在A班有一个名为
的活动


班级A

有TreeViewItems的TreeView。

  public   void  MyEvent_MouseLeftButtonUp( object  sender,MouseButtonEventArgs e)

{
// 有关TreeviewItem选择或焦点的一些代码

}





我的问题是我想在另一个班级访问这个Eventhandler



B级

i在这样的按钮点击事件处理程序中编写代码,



ClassA classAObj = new ClassA();





classAObj.treeviewItem.AddHandler(MouseLeftButtonUp,new MouseEventHandler(classAObj.MyEvent_MouseLeftButtonUp),true); //不工作



这里我也想要

classAObj.treeviewItem.IsSelected = true; //工作正常,但我想要 classAObj.treeviewItem.IsSelectionActive = true; //不工作



请让我知道任何答案



谢谢

解决方案

< blockquote>嗨gaganeti,



听起来你应该再次拿起你的.NET / WPF书:-)

另外一个好建议:了解VS在错误消息中告诉您的内容,尝试了解各种消息的含义(有时是明显的,有时不是)。



所以: item.IsSelectionActive = ... 将无效,因为您尝试访问没有公共设置器的属性 - 因此无法像这样设置它(您可以通过反射避开这个,但不要' t,班级设计师很可能知道当他限制访问这个属性时他在做什么 - 我猜它是计算的电话)。



关于事件处理:我不明白你是什么真的想要。 访问事件处理程序?你想要什么?处理事件或提升事件,或只访问处理程序(这是一种常规方法,所以只需将其公开)。在谈论.NET事件时总是会出现同样的问题,因为每个人都在混淆这些术语。



所以从你的例子我猜你想要访问处理程序方法本身。所以你的代码结构应该是这样的:



  class  A 
{
TreeView treeview = new TreeView();

public A()
{
treeview.MouseLeftButtonUp + = treeview_MouseLeftButtonUp;
}

public void treeview_MouseLeftButtonUp( object sender,MouseButtonEventArgs e){ / * 处理所有* / }
}

class B
{
TreeView treeview = new TreeView();

public B(A a)
{
// argh no,我不想再次编写这个处理程序,只是从A实例重用处理程序

treeview.MouseLeftButtonUp + = a.treeview_MouseLeftButtonUp;
}
}







这就是你想要的?



无论如何,这是一个非常奇怪的模式,我认为你应该考虑一些设计改变。在我看来,这种方法将违背所有优秀的OOP原则。



如果我错过了你(英语不是我的母语......),我很抱歉。 br $>


亲切的问候



Johannes


Hello All,

i have two classes,In Class A there is event called

Class A
There is a TreeView with TreeViewItems.

public void MyEvent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      
{
//some code regarding TreeviewItem select or Focus

}



my question is i want to access this Eventhandler in another class

Class B
i wrote code in button click event handler like this,

ClassA classAObj=new ClassA();


classAObj.treeviewItem.AddHandler(MouseLeftButtonUp,new MouseEventHandler(classAObj.MyEvent_MouseLeftButtonUp),true); //not working

here i also want
classAObj.treeviewItem.IsSelected=true; //working fine but i want classAObj.treeviewItem.IsSelectionActive=true; // not working

please let me know any answers

thanks

解决方案

Hi gaganeti,

Sounds like you should pick up your .NET/WPF book again :-)
Also a good advice: Learn what VS is telling you in the error messages, try to understand what the various messages mean (sometimes obviouse, sometimes not).

So: item.IsSelectionActive = ... won't work because you try to access a property without a public setter - so no way to set it like this (you can circumvent this by reflection, but don't, the class designer very likely knew what he was doing when he limited Access to this property - I'd guess it's "calculated" on call).

Regarding Event handling: I don't understand what you realy want. "Access a event-handler"?. What you want? HANDLE the Event or RAISE the Event, or ACCESS just the handler (which is a normal method, so just make it public). Always the same problems when talking about .NET events because everyone is mixing up the terms.

So from your example I would guess you want to ACCESS the handler method itself. So your code structure should look something like this:

class A
 {
     TreeView treeview = new TreeView();

     public A()
     {
         treeview.MouseLeftButtonUp += treeview_MouseLeftButtonUp;
     }

     public void treeview_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { /* handles all */  }
 }

 class B
 {
     TreeView treeview = new TreeView();

     public B(A a)
     {
         // argh no, I don't want to write this handler again, just reuse handler from A instance

         treeview.MouseLeftButtonUp += a.treeview_MouseLeftButtonUp;
     }
 }




Is this what you want?

Anyway this is quite a strange pattern and I think you should consider some design-change. This approach would be against all good OOP principles in my opinion.

Sorry if I have miss-understood you (English is not my native language...).

Kind regards

Johannes


这篇关于在另一个类中访问MouseLeftButtonUp EventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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