Silverlight MouseLeftButtonDown event does not fire

I can receive events MouseEnter, MouseLeaveand Click, but not MouseLeftButtonDownor MouseLeftButtonUp.

Here is my xaml

    <UserControl x:Class="Dive.Map.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
        <Canvas x:Name="LayoutRoot" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
            <Button x:Name="btnTest" Content="asdf" Background="Transparent"  MouseLeftButtonDown="btnTest_MouseLeftButtonDown"></Button>
        </Canvas>
    </UserControl>

And here is my code

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void btnTest_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
    {
        btnTest.Content = DateTime.Now.ToString();
    }

    private void LayoutRoot_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
    {
        e.Handled = false;
    }
}

What am I doing wrong?

+3
source share
3 answers

The element Button(or, more specifically, the superclass of the class ButtonBasefrom which it is derived) processes the event MouseLeftButtonDownitself to generate an event Click. Therefore, you cannot receive an event MouseLeftButtonDownfrom the standard one Button.

Is there a reason you are not using the event Click?

+7
source

If you add handlers with this code:

    dgv.AddHandler(DataGrid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(dgv_MouseLeftButtonDown), true);
    dgv.AddHandler(DataGrid.MouseLeftButtonUpEvent, new MouseButtonEventHandler(dgv_MouseLeftButtonUp), true);

, true "HandledEventsToo". , - ...

+4

- , MouseLeftButtonDown MouseLeftButtonUp Silverlight? , , OnMouseLeftButtonDown OnMouseLeftButtonUp. OnMouseLeftButtonDown Click , MouseLeftButtonDown , . , OnMouseLeftButtonUp MouseLeftButtonUp .

ClickMode Button. : Hover, Press, Release. "", . ClickMode , Click OnMouseLeftButtonUp, MouseLeftButtonDown MouseLeftButtonUp . ClickMode Hover, Click MouseEnter, .

0

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


All Articles