c# - When are tunneling and bubbling events useful in WPF? -


i understand how bubbling , tunneling works. however, i'm confused using them. here why:

i want handle mouse click event. bubble it, there mousedown and, tunnel it, there previewmousedown. however, mousedown doesn't mean user clicked control. may user pressed button , moved away cancel click. wouldn't want change if button not being clicked.

so question is, how bubbling/tunneling strategies useful?

if event listed routedeventargs, it's routed event. routed events support routingstrategy of bubble, tunnel, or direct. let's take @ event handler of button.click:

private void grid_click(object sender, routedeventargs e) {     messagebox.show("button test clicked!"); } 

there specified routedeventargs, it's routed event. because preview not specified in name, therefore bubble event. can demonstrated in following way:

<grid buttonbase.click="grid_click">     <button name="testbutton" width="100" height="30" content="test" /> </grid> 

when click on testbutton, event rise above grid, , displays message:

button test clicked!

usefulness of bubbling/tunneling strategies

tunneling

many of standard controls listen events, such keydown, mousedown, etc. example -datagrid control. want pressing enter key function called adding record. datagrid has keydown event, event not raised. have logic in tunnel event - previewkeydown, work before keydown event. same applies richtextboxcontrol.

bubbling

sometimes, need global handler specific event, worked controls in visualtree. naturally, direct event can not it. hence on stage comes bubbling event.

another reason ideology of wpf. button can contain anything: image, button, etc:

enter image description here

the user can click on textblock/image in button. how know click in button? that's right, of bubbling event.

for more information, please see:

understanding routed events , commands in wpf

edit

i changed little bit click handler:

private void grid_click(object sender, routedeventargs e) {     string message = "#" + eventcounter.tostring() + ":\r\n" +             " sender: " + sender.tostring() + ":\r\n" +             " source: " + e.source + ":\r\n" +             " original source: " + e.originalsource;      lstevents.items.add(message); } 

result of click on button:

enter image description here


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -