Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All,

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

Class A
There is a TreeView with TreeViewItems.
C#
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
Posted
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 10:31am    
What is "access a handler"? A handler is just a delegate instance, or a method.
Where did you get "AddHandler", from VB.NET?.. :-)
—SA

1 solution

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:

C#
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
 
Share this answer
 
Comments
GagKumar 25-Feb-14 10:17am    
Hi, thanks for Answer ,its about to reach ,but some clarification. i want to call MouseLeftButtonUp event in Button_Click eventhandler in another class, which you gave answer i tried ,but getting error "The event 'System.Windows.UIElement.MouseLeftButtonUp' can only appear on the left hand side of += or -=" ,
Can you give some idea please,
Sergey Alexandrovich Kryukov 25-Feb-14 10:29am    
Before "wanting" something, you should follow a good advice and learn how events work. It looks like you are too much confused. Before setting any goals, you should make yourself qualified enough for doing that.
—SA
johannesnestler 25-Feb-14 13:01pm    
must be a typo? check if you got the += operator right (this adds the handler) -= will remove it...
GagKumar 26-Feb-14 3:06am    
Thanks Johannenstler, got it, i accept your answer....
Sergey Alexandrovich Kryukov 25-Feb-14 10:29am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900