Can anyone raise any RoutedEvent in WPF?

In C #, events were always very protected: only the owner of the event could call them. However, in WPF, this seems completely different: anyone can throw any event at any time. To test this, I wrote the code below.

When I used RaiseEvent to raise Button.Click, the event above caught it. Is this the planned behavior of WPF events? Just let someone throw any events they want? Also, if so, what is the meaning of OwnerType when registering an event? I thought this was some kind of protection, but if it is, it’s bad, since anyone can access a public event and use the AddOwner function to add more owners.

Thank!

Xaml

<StackPanel Button.Click="ButtonBase_OnClick">
    <Button Name="RealButton">Real button</Button>
    <WpfWindow:VitalyControl MouseDown="UIElement_OnMouseDown">
      I am almost a button
    </WpfWindow:VitalyControl>
</StackPanel>

Code for

User control:

class VitalyControl : Label
{
    public VitalyControl()
    {
        this.MouseDown += new MouseButtonEventHandler(VitalyControl_MouseDown);
    }

    void VitalyControl_MouseDown(object sender, MouseButtonEventArgs e)
    {
        RaiseEvent(new RoutedEventArgs(Button.ClickEvent, this));
    }
}

And the handler:

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button was pressed");
    }
+3
1

RoutedEvents. , . , , " " msdn. , StackPanel Button.Click.

. "" , .

UPDATE:

, , UIElement . UI WinForms. CLR. .

+3

Source: https://habr.com/ru/post/1789799/


All Articles