我是新来的WPF。在我的WPF应用程序,我的Windows包含一个用户定义的子控件和用户定义的子控件又包含另外一个用户定义的子控件。现在从最内侧的子控件,在点击一个按钮,我想火在所有三个控件(即第一个大孩子的控制,第二个孩子的控制,第三个主要的控制和窗口)。事件
I'm new to WPF. In my WPF app, I have Windows which contain a user defined child control and that user defined child control again contains another user defined child control. Now from the inner most child control, on a button click, I want to fire events on all three controls (i.e. First Grand Child Control, Second Child Control, Third Main Control, and Window).
我知道这可以通过委托和事件冒泡来实现。你能告诉我怎么样?
I know this can be achieved through delegates and Event Bubbling. Can you please tell me how?
最重要的一块PF code为: 添加事件处理程序的静态UIElement.MouseLeftButtonUpEvent:
Most important piece pf code for that: Add the event handlers on the static UIElement.MouseLeftButtonUpEvent:
middleInnerControl.AddHandler(UIElement.MouseLeftButtonUpEvent , new RoutedEventHandler(handleInner)); //adds the handler for a click event on the most out
mostOuterControl.AddHandler(UIElement.MouseLeftButtonUpEvent , new RoutedEventHandler(handleMostOuter)); //adds the handler for a click event on the most out
该事件处理:
The EventHandlers:
private void handleInner(object asd, RoutedEventArgs e)
{
InnerControl c = e.OriginalSource as InnerControl;
if (c != null)
{
//do whatever
}
e.Handled = false; // do not set handle to true --> bubbles further
}
private void handleMostOuter(object asd, RoutedEventArgs e)
{
InnerControl c = e.OriginalSource as InnerControl;
if (c != null)
{
//do whatever
}
e.Handled = true; // set handled = true, it wont bubble further
}
上一篇:是否有一个XmlReader相当于HTML中的净?有一个、XmlReader、HTML
下一篇:在DataSnapshot.getValue()时间戳Android的聊天崩溃时间、DataSnapshot、getValue、Android