Home>

When there is an object with each event (button1 and button2, CanvasControl), if the topmost object (CanvasControl) is almost transparent (Background = "# 01FFFFFF"), Button1 or Button2 and CanvasControl What if I want to trigger both mouse events at the same time?

In the following cases, only the CanvasControl event occurs and the Button1 and Button2 events below it do not occur.

I do n’t even see the direction and I ’m worried.

<Window x: Class = "WpfObjectAutoMake01.MainWindow"
        xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
        xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc: Ignorable = "d"
        Title = "MainWindow" Height = "450" ​​Width = "800">
    <Grid>
        <Button x: Name = "button1" Content = "Button" HorizontalAlignment = "Left" Margin = "34,27,0,0" VerticalAlignment = "Top" Width = "75" Click = "button1_Click" />
        <Button x: Name = "button2" Content = "Button" HorizontalAlignment = "Left" Margin = "134,27,0,0" VerticalAlignment = "Top" Width = "75" Click = "button2_Click" />
        <Canvas Name = "CanvasControl" Background = "# 01FFFFFF" MouseDown = "CanvasControl_MouseDown" />
    </Grid>
</Window>

Added November 27, 2019
Fixed the button position overlapping

  • Answer # 1

      

    Because I want to generate an event different from Button regardless of Button, I thought that CanvasControl needs to cover Button.

    I want an event with or without buttons.
    What aboutPreviewMouseDown? It is difficult to come before Click.

    
      
        
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Input;
    namespace Questions225607
    {
        public partial class MainWindow: Window
        {
            public MainWindow () =>InitializeComponent ();
            private void grid1_PreviewMouseDown (object sender, MouseButtonEventArgs e)
                =>Debug.WriteLine ($"grid1_PreviewMouseDown: {e.GetPosition (this)}");
            private void button1_Click (object sender, RoutedEventArgs e)
                =>Debug.WriteLine ("button1_Click");
            private void button2_Click (object sender, RoutedEventArgs e)
                =>Debug.WriteLine ("button2_Click");
        }
    }