Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event source vs original source

Tags:

c#

events

wpf

xaml

I'm reading C# WPF book and in routed events chapter, event has 2 same properties Source and OriginalSource. I did not see the difference between them:

Xaml:

<Button Name="Ok" Click="Ok_Click"/>

Code behind:

private void Ok_Click(object sender, RoutedEventArgs e)
{
        bool flag = false;
        var source = e.Source;
        var originalSource = e.OriginalSource;
        if (source == originalSource)
        {
            flag = true;
        }
}

and flag property true here, can some one explain why 2 same property or in this case this properties has no effects? or where can we see use case for this properties?

like image 612
Jamaxack Avatar asked Feb 24 '15 05:02

Jamaxack


1 Answers

A control can have other controls within it as children. When you subscribe to a event from the control the parent you subscribed to is likely to be the e.Source however if the control has children and the child is the one that raised the event then the OriginalSource will be the child that raised the event.

A common use case is subscribing to the KeyDown event on a DataGrid. The source may be the grid but the original source might be the cell (or some component in the cell)

like image 91
Scott Chamberlain Avatar answered Nov 19 '22 17:11

Scott Chamberlain