Search code examples
c#.netwpfinternals

WPF internal : Why UserControl is overriding AdjustBranchSource?


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?


Solution

  • 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.