views:

221

answers:

2

By Using reflector you can see that WPF UserControl is overriding AdjustBranchSource.

    internal override void AdjustBranchSource(RoutedEventArgs e)
{

   e.Source = this;

}

My very own problem regards inconsistency caused by that issue. When an element is based inside a user control or outside. The Source parameter behaves differently. Which surprises me the source should always be the element in target by the RoutedEvent.

The question is why was it implemented like that?

+1  A: 

The source of a routed event can change throughout the routing of the event. I'm not entirely sure why UserControl changes it, but can you not just use the OriginalSource property on RoutedEventArgs instead?

HTH, Kent

Kent Boogaart
I might, it is a pain. You will get the ButtonChrome or another element like part. After that you will need to deduce the element from the visual tree. ain't pretty.
ArielBH
+3  A: 

This kinda makes sense. If you treat the UserControl as a black box then you shouldn't know what controls are on it, and thus the source of an event.

If you need to distinguish between different buttons on the UserControl then the UserControl should have it's own events which the buttons trigger. That way from the outside it looks like the right event and the user of the UserControl doesn't need to know which button did which event.

To give an example, on a listbox, do you need to know that the down-scroll button was the button that sent the original event? Or do you just need to know that a scroll-down event was triggered.

Cameron MacFarland