RoutedEventArgs的Source与OriginalSource

路由事件是沿着VisualTree传递。VisualTree与LogicalTree区别在于:LogicalTree的叶子是构成用户界面的控件,而VisualTree要连接控件中的细微结构也算上。

“路由事件在VisualTree上传递”本意上是说“路由事件的消息在VisualTree上传递”,而路由事件的消息包含在RoutedEventArgs实例中。
RoutedEventArgs有两个属性Source和OriginalSource,这两个属性都表示路由事件传递的起点,那么他们之间有什么区别那?
Source表示的是LogicalTree上消息的源头;
OriginalSource则表示VisualTree上的源头。

下面看一个例子:
首先定义一个用户控件:

<UserControl x:Class="WpfWindows8.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfWindows8"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="5">
        <Button x:Name="innerButton" Width="80" Height="80" Content="OK" />
    </Border>
</UserControl>

这个控件包含一个名为innerBtton的Button控件,将此控件添加到主窗体上:

<Window x:Class="WpfWindows8.WinMain8_3_3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfWindows8"
        mc:Ignorable="d"
        Title="Source v.s.OriginalSource" Height="180" Width="300" WindowStyle="ToolWindow">
    <Grid>
        <local:MyUserControl x:Name="myUserControl" Margin="10"/>
    </Grid>
</Window>

再在后台为主窗体添加对Button.Click路由事件的侦听:

 public WinMain8_3_3()
        {
            InitializeComponent();

            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.Button_Click));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string strOriginalSource = string.Format("VisualTree start point: {0}, type is {1}",
                (e.OriginalSource as FrameworkElement).Name, e.OriginalSource.GetType().Name);

            string strSource = string.Format("LogicalTree start point: {0}, type is {1}",
                (e.Source as FrameworkElement).Name, e.Source.GetType().Name);

            MessageBox.Show(strOriginalSource + "\r\n" + strSource);
        }

运行结果如下:
这里写图片描述这里写图片描述

转角() 19241icShamCode SharZiptib.Sh在ICSharpCode.SharpZipLibZip.Compre!ssion.InfaterDynHeac在ICSharpCode.SharpZipLib.Zip.Comprssion.Inflater.Decode(在lCSharpCode.SharpZipLib.Zip.Compression.Inflater.Inflate(Bytel buffer, int32 offset, Int32offset int32 count在ICSharpCode.SharpZipLib.Zip.Compn.Streams.InflaternputStream.Read(Bytel buffer, lnt32 在ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(Stream source, Stream destination, Bytel buffe在lCSharpCode.SharpZipLib.Zip.FastZip.ExtractFileEntry(ZipEntry entry, String t在iCSharpCode.SharpZipLib.Zip.FfastZip.ExtractEntry(ZipEntry entry)在ICSharpCodeSharpZipLib.Zip.fastZip.ExtractZigate confirmDelegate, String filefilter, String directoryFilter, Boolean restoreDateTime, BooleaisStreamOwner) 速率:0.00/S 试验方案 在ICSharpCode.SharpZiplib.Zip.FastZip.ExtractZip(String zipFileName在 Torque.MainWindow.OpenTestDatafromXml(String file) 位置 E:\软件任务\TCH FTAS数]\TCH FTAS V4.0.3.0\Torque\MainWindowTestcs:行号1924在Torque.MainWindow.btnOpenTestDatiect sender, RoutedEventArgs e) 位霅 E:\软件任务\TCH FTAS[J\TCH FTAS V4.0.3.0\Torque\MainWindow.xaml.cs行号4469t sysrem windows eoeeaevemoraennaeniprorobeer opieet noare eouneaaranyes brin h kati在System.Windows.RoutedEventHand在System,Windows.UlElement.Rasenqencooment.RaiseEvent(RoutedEventArgs e ⊙ 试验 ○ 编辑有效力矩 在System.Windows.UlEler在System.Windows.ControlsButton.OnClick( 在System,.Windows.Controls,Primitives.ButtonBase.OnClick( 在System.Windows.ControlsPrimitives.ButtonBase.OnMouseLeftB 在System.Windows.UlElen nUpThunk(Obiect sender. Mo 在System.Windows.Input.MouseButtonEventArgs.Inv okeEventHa 在System.Windows.RoutedEventArgs.nvokeHandler(Delegate handler, Object 在System.Windows.RoutedEventHandlerinfo.n 在System,Windows.EventRoute.nvokeHandlerslm! 在 System.Windows.UlElementReRai. ventAs(Der ect target, Rou lObiect source,RoutedEventArgs args, Bo0 在System.Windows.UlElement.OnMouseUpThunk(C 在System.Windows.input.MouseButto ventArgs e) 在System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 在System.Windows.RoutedEventHanderinfo 在System.Windows.EventRoute.InvokeHand oiect source, Routec dEventArgs args, Boolean reRaised intArgs args) 在System.Windows.UlElement.RaiseEve )bject sende 在System.Windows.UlElementRaiseTrustedEvent(RoutedEventArgs args) 在System.Windows.UlElement.RaiseEvent(Route dEventArgs args 在System.Windows.nputInputManager,ProcessStagingArea 在System.Windows.inputinputManager.Processinput(! tArgs input 在 System.Windows,in! 在System.Windows.interop.HwndMouseln it(lntPtr hwnd, InputMode mode, int32 timestamp, RawMol iseActions actions, Int32 x, Int32 y, int32 wheel) age(intPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handiec 在System.Window e(intPtr hwnd, int32 msg, intPtr wParam, IntPtr lParam, Boolean& handled) 在 MS.in32.HwndWrapper.WndProc(lintPtr hwnd, int32 msg, intPtr wParam, IntPtr IParam, Boolean& handied) 在MSWin32.HwndSubdass.DispatcherCalbackOperation(Object o) 在 System.indows,Threading,.ExceptionWrapper.nternalRealCall(Delegate callback, Object args, Int32 numArgs) 在System,WNindowsThreading.ExceptionWrapper.TryCatchWhe (Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 确定 C 室温15次 螺纹案固件试验分 入@小英国
最新发布
10-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值