Why does Source for ContextMenuOpening behave differently for Canvas and UserControl?

I have a simple window:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:self="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="435" Width="613">
    <StackPanel>
        <Canvas Name="canvas">
            <self:Red />
        </Canvas>
        <UserControl Name="uc">
            <self:Blue />
        </UserControl>
    </StackPanel>
</Window>

      

Red

and are Blue

very simple UserControls

:

<UserControl x:Class="WpfApplication1.Blue"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Rectangle Fill="Blue" Width="100" Height="100" />
    </Grid>
</UserControl>

      

I created some ContextMenus:

public MainWindow()
{
    InitializeComponent();

    canvas.ContextMenu = new ContextMenu();
    canvas.ContextMenuOpening += (sender, e) =>
    {
        System.Diagnostics.Debug.WriteLine(e.Source.GetType());
    };

    uc.ContextMenu = new ContextMenu();
    uc.ContextMenuOpening += (sender, e) =>
    {
        System.Diagnostics.Debug.WriteLine(e.Source.GetType());
    };
}

      

If I open the context menu to Canvas

, it Source

will Red

, but if I open it to UserControl

, it Source

will UserControl

.
Any idea why?
I found this on MSDN :

ContextMenu is itself a FrameworkElement derived class, but this event will not be raised from the context menu that opens as a source. The event is raised from the element that "owns" the context menu as a property ...

If I understand correctly, it Source

should be Canvas

in the first case, but it is not.

+3


source to share


1 answer


This behavior is pretty well documented in the MSDN documentation for the RoutedEventArgs.OriginalSource property :

Correcting the source with different content elements and models varies from class to class. Each class that configures event sources tries to anticipate which source is most useful for reporting for most of the input scenarios and scenarios the class is intended for, and then sets that source as the source. If this source is not relevant to your event handling, try checking the original source instead to see if another source is more appropriate.



This is exactly what the UserControl class does, it fixes the Source property in the AdjustBranchSource () method .

So, as hinted at by the quoted text, you might be looking for the OriginalSource property to make the code behave in a similar way, in both cases you will get a reference to the Rectangle.

+1


source







All Articles