www.pudn.com > SLRoutedEvent.zip > MainPage.xaml.cs, change:2011-04-11,size:2653b


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
 
namespace SLRoutedEvent 
{ 
    public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            InitializeComponent(); 
        } 
        #region 路由事件的原理 
        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
        { 
            //正在调用事件处理程序的对象 
            FrameworkElement grid = sender as FrameworkElement; 
            //获取到触发此次事件的对象 
            FrameworkElement ellipse = e.OriginalSource as FrameworkElement; 
            MessageBox.Show("引发事件的子对象名是:" + ellipse.Name + "----子对象事件冒泡上来触发并且产生事件的父对象名是:" + grid.Name); 
        } 
        private void ellipse2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
        {            
            //获取到触发此次事件的对象 
            FrameworkElement ellipse = e.OriginalSource as FrameworkElement; 
            //通过e.Handled设置为ture表示路由事件已处理拦截了事件,不必冒泡到父对象的LayoutRoot_MouseLeftButtonDown去处理。 
            MessageBox.Show("引发事件的对象名是:" + ellipse.Name + ",路由事件被拦截"); 
            e.Handled = true; 
        } 
        #endregion 
 
 
 
        #region 为GridB控件添加一个路由事件处理程序,并且设置一直都要处理这个路由事件 
        private void UserControl_Loaded(object sender, RoutedEventArgs e) 
        { 
            //为GridB控件添加一个路由事件处理程序,并且设置一直都要执行此路由事件 
            this.GridB.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(GridB_MouseLeftButtonDown), true); 
        } 
 
        private void GridB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
        { 
            //处理这次事件的对象 
            FrameworkElement grid = sender as FrameworkElement; 
            //获取到触发事件的对象 
            FrameworkElement ellipse = e.OriginalSource as FrameworkElement; 
            MessageBox.Show("引发事件的子对象名是:" + ellipse.Name + "----子对象事件冒泡上来触发并且产生事件的父对象名是:" + grid.Name); 
        } 
        #endregion 
 
    } 
}