WPF InputHitTest不能检测到单击标签

时间:2019-08-26 06:31:25

标签: c# wpf

我上面有一个画布以及行和标签对。它们具有相同的标签。当我单击线条时,它会检测到并删除带有相同标签的线条和标签。但是,当我单击标签时,它没有。

这是我的检测代码:

private void MainCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var mousePosition = Mouse.GetPosition(mainCanvas);

    Line c1 = mainCanvas.InputHitTest(mousePosition) as Line;

    if (c1 != null && c1.Tag != null && c1.Tag.ToString().StartsWith("dist"))
    {
        string tag = c1.Tag.ToString();
        var list = mainCanvas.Children
                             .Cast<FrameworkElement>()
                             .Where(a => a.Tag != null && a.Tag.Equals(tag))
                             .ToList();

        foreach(var i in list)
        {
            mainCanvas.Children.Remove(i);
        }
    }
    else
    {
        Label c2 = mainCanvas.InputHitTest(mousePosition) as Label;

        if (c2 != null && c2.Tag != null && c2.Tag.ToString().StartsWith("dist"))
        {
            string tag = c2.Tag.ToString();
            var list = mainCanvas.Children
                                 .Cast<FrameworkElement>()
                                 .Where(a => a.Tag != null && a.Tag.Equals(tag))
                                 .ToList();

            foreach (var i in list)
            {
                mainCanvas.Children.Remove(i);
            }
        }
    }
}

0 个答案:

没有答案