C# WPF - событие MouseDown для перекрывающихся элементов (прямоугольник) - PullRequest
0 голосов
/ 31 января 2020

Я пытаюсь вызвать оба события щелчка, когда нажимаю на перекрывающуюся область (отмечена желтым цветом). В настоящее время, когда я нажимаю на эту область, только передний элемент вызывает функцию.

Rectangles

    <Rectangle Height="100" Width="100" MouseDown="Button_Click" StrokeThickness="1" Stroke="Black" Fill="Transparent" />
    <Rectangle Height="100" Width="100" Margin="396,209,297.6,111" MouseDown="Button_Click_1" StrokeThickness="1" Stroke="Black" Fill="Transparent"/>

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button_Click");
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button_Click_1");
    }

Возможно, кто-то может помочь. Спасибо!

1 Ответ

1 голос
/ 31 января 2020

Это может вам помочь

<Canvas x:Name="canvas1" PreviewMouseDown="canvas1_PreviewMouseDown">
    <Rectangle Height="100" Tag="Rectangle1" Width="100"  StrokeThickness="1" Stroke="Black" Fill="Transparent" />
    <Rectangle Height="100" Tag="Rectangle2" Width="100" Margin="60,60,297.6,111"  StrokeThickness="1" Stroke="Black" Fill="Transparent"/>
</Canvas>

Код возврата:

        private List<HitTestResult> hitResultsList = new List<HitTestResult>();

        private void canvas1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (canvas1.Children.Count > 0)
            {
                // Retrieve the coordinates of the mouse position.
                Point pt = e.GetPosition((UIElement)sender);

                // Clear the contents of the list used for hit test results.
                hitResultsList.Clear();

                // Set up a callback to receive the hit test result enumeration.
                VisualTreeHelper.HitTest(canvas1,
                                    new HitTestFilterCallback(MyHitTestFilter),
                                    new HitTestResultCallback(MyHitTestResult),
                                    new PointHitTestParameters(pt));

                // Perform actions on the hit test results list.
                if (hitResultsList.Count > 0)
                {
                    string msg = null;
                    foreach (HitTestResult htr in hitResultsList)
                    {
                        Rectangle r = (Rectangle)htr.VisualHit;
                        msg += r.Tag.ToString() + "\n";
                    }
                    //Message displaying the tag of all the rectangles 
                    //under the mouse when it was clicked.
                    MessageBox.Show(msg);
                }
            }
        }

        // Filter the hit test values for each object in the enumeration.
        private HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
        {
            // Test for the object value you want to filter.
            if (o.GetType() == typeof(Label))
            {
                // Visual object and descendants are NOT part of hit test results enumeration.
                return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
            }
            else
            {
                // Visual object is part of hit test results enumeration.
                return HitTestFilterBehavior.Continue;
            }
        }

        // Add the hit test result to the list of results.
        private HitTestResultBehavior MyHitTestResult(HitTestResult result)
        {
            //Filter out the canvas object.
            if (!result.VisualHit.ToString().Contains("Canvas"))
            {
                hitResultsList.Add(result);
            }
            // Set the behavior to return visuals at all z-order levels.
            return HitTestResultBehavior.Continue;
        }

Вот результат:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...