Event source sender source originalSource

Source: Internet
Author: User

When I first started using WPF, I was confused about the relationship between the three event sources. After testing, I finally got a bit clear:

The sender of the sender event is white-out who calls the event processor.

If the Source event source is a raise event that does not use a routing event, the sender and Source are the same object.

OriginalSource is also the event Source, but it is different from Source because it is the Source of the original event, which truly inspires the Source of the event. When it is not encapsulated, the two are the same object.

The following example shows the obvious difference.

1. Custom Controls

XAML

<UserControl x:Class="SourceTest.UserControl1"             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"              mc:Ignorable="d"              d:DesignHeight="300" d:DesignWidth="300">    <Grid>        <StackPanel>            <Button x:Name="testButton" Content="testButton" Click="testButton_Click" />        </StackPanel>    </Grid></UserControl>
CodeBehind

Using System. windows; using System. windows. controls; namespace SourceTest {// <summary> // interaction logic of UserControl1.xaml /// </summary> public partial class UserControl1: UserControl {public UserControl1 () {InitializeComponent ();} private void testButton_Click (object sender, RoutedEventArgs e) {MessageBox. show (string. format ("sender Type: {0} \ n \ r source Type: {1} \ n \ r originalSource Type: {2}", sender. getType (). name, e. source. getType (). name, e. originalSource. getType (). name), "subform message ");}}}

2. Main form

XAML

<Window x:Class="SourceTest.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:SourceTest"        Title="MainWindow" Height="100" Width="200">    <Grid>        <local:UserControl1 x:Name="test1"/>    </Grid></Window>
CodeBehind

Using System. windows; using System. windows. controls; namespace SourceTest {// <summary> // MainWindow. interaction logic of xaml // </summary> public partial class MainWindow: Window {public MainWindow () {InitializeComponent (); this. addHandler (Button. clickEvent, new RoutedEventHandler (s, e) =>{ MessageBox. show (string. format ("sender Type: {0} \ n \ r source Type: {1} \ n \ r originalSource Type {2}", s. getType (). name, e. source. getType (). name, e. originalSource. getType (). name), "main form message ");}));}}}

3. click the button to run the result.

 

The difference between a routing event and a traditional CLR event is that the sender of a CLR event is a control, the responder is an event processor, and there is an obvious subscription relationship between the sender and the responder.

The object of the event to be triggered by the routing event is separated from the object to process the event. The source is only responsible for initiating the event.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.