الاثنين، 6 يونيو 2011

WPF Routed events

Routed events are events that are designed to work well with a tree of elements. When a routed event is raised, it can travel up or down the visual and logical tree, getting raised on each element in a simple and consistent fashion, without the need for any custom code.


Routed events are a new infrastructure provided by WPF which allows events to tunnel down the visual tree to the target element, or bubble up to the root element.  When an event is raised, it “travels” up or down the visual tree invoking handlers for that event on any element subscribed to that event it encounters en route.  Note that this tree traversal does not cover the entire visual tree, only the ancestral element chain between the root element and the element which is the target of the event.
It is common that one logical routed event is represented by two actual events, one tunneling and one bubbling.  The naming convention for tunneling events is PreviewXYZ, where XYZ is the name of the bubbling event.  For example, PreviewMouseLeftButtonDown and MouseLeftButtonDown are a pair of routed events used to notify elements in the visual tree that the user has depressed the left mouse button.  Not all routed events have a tunneling event, and some events do not tunnel or bubble at all.  Those are referred to as directevents, and they are essentially the same as standard CLR events.




Benefits
1.       One very important benefit of routed events is that a high-level visual element in a UI need not explicitly hook the same event on all of its descendants, such as MouseMove.  Instead it can hook the event on itself, and when the mouse moves over one of its descendants, the high level element will be notified appropriately.
2.       All levels of the visual tree can execute code in response to events of their descendants, without expecting the descendant to notify them when the event fires.  

Handling WPF Control Events
Steps #1
Steps #2









public class Button : ButtonBase
{
// The routed event
public static readonly RoutedEvent ClickEvent;
static Button()
{
// Register the event
Button.ClickEvent = EventManager.RegisterRoutedEvent("Click",RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Button));
}
// A .NET event wrapper (optional)
public event RoutedEventHandler Click
{
add { AddHandler(Button.ClickEvent, value); }
remove { RemoveHandler(Button.ClickEvent, value); }
}

}
}


2 التعليقات:

Eng.Islam Nigm يقول...

السلام عليكم
تحياتي لك م/ أحمد ربيع
أحب أن اعلمك اني متابع جديد لحلقاتك التعليمية المختلفة والتي اراها مميزة بشكل كبير .
احب ان ادعو لك بان يجزيك واهالك خيرا بما تقدمه للاخرين.

بداية معرفتي بك وبحلقاتك التعليمية من خلال موقع الفريق العربي للبرمجة.

لي استفسار بسيط اخي وهو عن كتابك الخاص بال ASP.NET هل هو مطبق علي Visual studio 2008 or visual web developer express او اصدارات اخري .
أرجو الإفادة للبدأ في دراسة الكتاب .
شكرا ولك جزيل الشكر .
أتمني أكون من تلاميذك .

Ahmed Rabie El Bohoty يقول...

وعليكم السلام ورحمة الله وبركاتة

يتم شرح الحلقات والفيديو
Visual studio 2010

امابالنسبة للكتاب فهو على
Visual studio 2008


وبارك الله فيكم