标签:style blog http color 使用 os
 private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("我到达了:" + (sender as FrameworkElement).Name+"原来是你激发了我:"+(e.Source as FrameworkElement).Name);
        }
public class ReportLocationEventArgs:RoutedEventArgs
    {
        public ReportLocationEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }
        public string Location { set; get; }
    }
public class ReportLocation : Button//继承Button  制造相关事件
   {
       //声明并定义路由事件
       public static readonly RoutedEvent ReportLocationEvent = EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportLocationEventArgs>), typeof(ReportLocation));
       //CLR事件包装器
       public event RoutedEventHandler ReportTime
       {
           add { this.AddHandler(ReportLocationEvent, value); }
           remove { this.RemoveHandler(ReportLocationEvent, value); }
       }
       //重写单击事件,激发路由事件
       protected override void OnClick()
       {
           base.OnClick();
           ReportLocationEventArgs Myargs = new ReportLocationEventArgs(ReportLocationEvent, this);
           Myargs.Location = this.Name;
           this.RaiseEvent(Myargs);
           
       }
            
   }
<Window x:Class="WPF路由事件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF路由事件"
        Title="MainWindow" Name="Window" Height="359" Width="255" local:ReportLocation.ReportTime="ReportTimeHandler">
    <Grid x:Name="GridRoot" Background="Lime" local:ReportLocation.ReportTime="ReportTimeHandler" >
           <Grid x:Name="GridA" Margin="10" Background="Blue"  local:ReportLocation.ReportTime="ReportTimeHandler">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
            <Canvas x:Name="CanvasLeft" Grid.Column="0" Background="Red" Margin="10"  local:ReportLocation.ReportTime="ReportTimeHandler">
                <local:ReportLocation x:Name="ButtonLeft" Width="65" Height="100" Content="Left"  local:ReportLocation.ReportTime="ReportTimeHandler"></local:ReportLocation>
            </Canvas>
             <Canvas x:Name="CanvasRight" Grid.Column="1" Background="Yellow" Margin="10">
                <local:ReportLocation x:Name="ButtonRight" Width="65" Height="100" Content="Right"  local:ReportLocation.ReportTime="ReportTimeHandler"></local:ReportLocation>
            </Canvas>
            <ListBox Name="listbox" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="134" Margin="10,165,0,0" VerticalAlignment="Top" Width="207"></ListBox>
            </Grid>
        </Grid>
</Window>
标签:style blog http color 使用 os
原文地址:http://www.cnblogs.com/fuchongjundream/p/3836431.html