Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF-重叠元素的鼠标向下事件(矩形)_C#_Wpf - Fatal编程技术网

C#WPF-重叠元素的鼠标向下事件(矩形)

C#WPF-重叠元素的鼠标向下事件(矩形),c#,wpf,C#,Wpf,我尝试在单击鼠标时触发两个单击事件 重叠区域(黄色标记)。 目前,当我在点击这个地区只有前面 元素调用该函数 私有无效按钮\u单击(对象发送者,路由目标e) { MessageBox.Show(“按钮点击”); } 私有无效按钮\u单击\u 1(对象发送者,路由目标) { MessageBox.Show(“按钮单击1”); } 也许有人能帮忙。谢谢 这可能对你有帮助 <Canvas x:Name="canvas1" PreviewMouseDown="canvas1_PreviewM

我尝试在单击鼠标时触发两个单击事件 重叠区域(黄色标记)。 目前,当我在点击这个地区只有前面 元素调用该函数


私有无效按钮\u单击(对象发送者,路由目标e)
{
MessageBox.Show(“按钮点击”);
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
MessageBox.Show(“按钮单击1”);
}
也许有人能帮忙。谢谢

这可能对你有帮助

<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;
        }
private List hitsresultslist=new List();
私有void canvas1_PreviewMouseDown(对象发送器,鼠标按钮ventargs e)
{
如果(canvas1.Children.Count>0)
{
//检索鼠标位置的坐标。
点pt=e.GetPosition((UIElement)发送器);
//清除用于命中测试结果的列表的内容。
hitResultsList.Clear();
//设置回调以接收命中测试结果枚举。
VisualTreeHelper.HitTest(canvas1,
新HitTestFilterCallback(MyHitTestFilter),
新HitTestResultCallback(MyHitTestResult),
新的PointHitTestParameters(pt));
//在命中测试结果列表上执行操作。
如果(hitResultsList.Count>0)
{
字符串msg=null;
foreach(hitResultsList中的HitTestResult htr)
{
矩形r=(矩形)htr.VisualHit;
msg+=r.Tag.ToString()+“\n”;
}
//显示所有矩形标记的消息
//单击鼠标时,在鼠标下方。
MessageBox.Show(msg);
}
}
}
//筛选枚举中每个对象的命中测试值。
专用HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
//测试要筛选的对象值。
if(o.GetType()==typeof(标签))
{
//可视对象和子体不是命中测试结果枚举的一部分。
return HitTestFilterBehavior.continuesKipself和children;
}
其他的
{
//可视对象是命中测试结果枚举的一部分。
返回HitTestFilterBehavior。是否继续;
}
}
//将命中测试结果添加到结果列表中。
私有HitTestResultBehavior MyHitTestResult(HitTestResult)
{
//过滤掉画布对象。
如果(!result.VisualHit.ToString()包含(“画布”))
{
hitResultsList.Add(结果);
}
//将行为设置为在所有z顺序级别返回视觉效果。
返回HitTestResultBehavior。是否继续;
}
结果如下:


您可能想看看。就是这样。非常感谢。
        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;
        }