WPF: Can't get to original source from ExecutedRoutedEventArgs
- by Ikhail
I have a problem getting to the original source of a command using ExecutedRoutedEventArgs.
I'm creating a simple splitbutton, in which a menu will appear below a dedicated button, as another button is pressed. When I click a menuitem in the appearing menu a command is fired. This command is registered on the splitbutton. And the idea is to get to the menuitem beeing clicked, by using the ExecutedRoutedEventsArgs.
Ok, now the problem. If I choose to have the popup menu shown by default (IsOpen="True") and I click one of the menuitems I can get to the originalsource (thus the menuitem) from the ExecutedRoutedEventArgs - no problem. However, if I first click the button to show the menu and THEN click on a menuitem, the originalsource of the command will be the button instead of the MenuItem!
Here's the controltemplate for the splitbutton:
<ControlTemplate TargetType="{x:Type usc:SplitButton}">
  <StackPanel Orientation="Horizontal">
    <Button Name="mybutton">
      <StackPanel>
        <Popup usc:SplitButton.IsPopup="True" IsOpen="True" Name="myPopup" PlacementTarget="{Binding ElementName=mybutton}" StaysOpen="False" Placement="Bottom">
        <Border BorderBrush="Beige" BorderThickness="1">
          <Menu Width="120">
            <MenuItem Header="item1" Command="usc:SplitButton.MenuItemClickCommand" />
            <MenuItem Header="item2" />
            <MenuItem Header="item3" />
           </Menu>
         </Border>
         </Popup>
         <TextBlock Text="MySplitbutton" />
        </StackPanel>
          </Button> 
      <Button Content="OK" Command="usc:SplitButton.ShowMenuCommand" />
   </StackPanel>
</ControlTemplate>
The OK button fires a ShowMenuCommand on the SplitButton, which sets the IsOpen property on the Popup to True.
Any ideas why the OK button (after having activated the menu) is the OriginalSource when a menuitem is clicked?
Thanks.