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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With