Responding to WPF Click Event in Custom Data Control

Hope this makes sense.

I have created several custom WPF controls. The lowest level element is "PostItNote.xaml". Then I have a NotesGroup.xaml file that has an ItemsControl associated with a List of PostItNotes. Apart from this, I have a ProgrammerControl.xaml file. Each ProgrammerControl has a grid with four different custom NotesGroups (and each Notes group contains 0-many PostItNotes.

Then I have a main window. It also has an ItemsControl associated with a Programmers list.

So you get a high level visual representation of the list of programmers, each programmer has four ticket groups, each ticket group has many PostItNotes.

The problem I am facing is that I want to respond to the mouse click event in my mainWindow file by file.

I can add MouseClick event to my PostItNote.xaml.vb file and receive it when user clicks PostItNote and I can re-raise the event; but I cant get NotesGroup to listen for this event. I'm not sure if this is even the correct approach.

When the user clicks PostItNote I'm going to make a bunch of type of business logic where the PostItNote control has no reference to / doesn't know about it.

Can anyone point me in the right direction?

+2


source to share


2 answers


You have several options:



  • Use PreviewXXX events, which are fired during the "tunneling" phase of WPF event routing . Parent controls can always view events passing through them to children.
  • Take a more advanced approach to hooking up events using the AddHandler method, to which you can pass a "handledEventsToo" parameter, which basically means you want to know when an event happened "inside" you, even if some descendant element handled the event itself by oneself.
+1


source


I'm going to take a flyer here. You probably don't want to handle an event that is high; really doesn't care. You catch the event at lower levels, which is inevitable. Consider calling a routed command from the PostItNote click event handler.

Routed commands fail and go through the tree. You might have an architecture where a high-level handler can listen for a boolean event (perhaps opening a postit post?). The handler does not need to worry about where the command comes from for this. You might have clicked something, it might be a button click on the toolbar. Both are valid scenarios.



It looks like you are creating some kind of user interface, right? You want your app to respond to user interaction. It is used for this RoutedCommands

.

+1


source







All Articles