0

所以我在画布上放置了两种类型的对象:矩形和文本框。我的问题如下:我需要像橡皮擦一样用 Mousedown 删除矩形和文本框。如果我只在我的屏幕上放置一个矩形,我只能删除一个矩形,如果我只在我的屏幕上放置一个文本框,我只能删除一个文本框。Mousedown但是我需要用(鼠标)在我的屏幕上删除它们,例如:在我的屏幕上它们是 2 个矩形和 1 个textbox,我单击它们并得到以下错误:无法转换 System.Windows.Controls 类型的对象。 TextBox 变成 System.Windows.Shapes.Rectangle 类型

我知道这是一个选角问题,但我尝试了很多东西。

这是我目前的代码:

class Eraser
{
    private Label status;
    private List<Rectangle> kaders;
    private List<TextBox> textboxen;
    private Canvas window;

    public Eraser(Canvas win, Label status)
    {
        this.status = status;
        this.window = win;
        //Application.Current.MainWindow.Cursor  = new Cursor(AppDomain.CurrentDomain.BaseDirectory + @"../../Icons/eraser.cur");
        //Mouse.OverrideCursor = newCursor;
        //Mouse.OverrideCursor = Application.Current.MainWindow.Cursor;
        textboxen = new List<TextBox>();
        kaders = new List<Rectangle>();
        foreach (Object element in win.Children)
        {
            if (element is Rectangle)
            {
                Mouse.RemoveMouseDownHandler(window, item_Mousedown);
                verzamelKaders();
            }
            else if (element is TextBox)
            {
                Mouse.RemoveMouseDownHandler(window, Element_Mousedown);
                verzamelTextboxen();
            }
        }

    }
    private void verzamelKaders()
    {
        Mouse.RemoveMouseDownHandler(window, item_Mousedown);

        foreach (Rectangle element in window.Children)
        {
            if (element.GetType() == typeof(Rectangle))
            {
                Mouse.RemoveMouseDownHandler(window, item_Mousedown);
                kaders.Add(element);
                element.MouseDown += new MouseButtonEventHandler(Element_Mousedown);
            }
            else
            {
                element.MouseDown += new MouseButtonEventHandler(item_Mousedown);
            }
        }

    }
    private void verzamelTextboxen()
    {

        Mouse.RemoveMouseDownHandler(window, Element_Mousedown);


        foreach (TextBox item in window.Children)
        {
            if (item.GetType() == typeof(TextBox))
            {
                Mouse.RemoveMouseDownHandler(window, Element_Mousedown);
                textboxen.Add(item);
                item.PreviewMouseDown += new MouseButtonEventHandler(item_Mousedown);
            }
            else
            {
                item.MouseDown += new MouseButtonEventHandler(Element_Mousedown);
            }

        }

    }
    private void Element_Mousedown(object sender, MouseButtonEventArgs e)
    {
        Mouse.RemoveMouseDownHandler(window, item_Mousedown);
        Rectangle removekader = new Rectangle();
        removekader = (Rectangle)sender;
        window.Children.Remove(removekader);

    }
    private void item_Mousedown(object sender, MouseButtonEventArgs e)
    {

        Mouse.RemoveMouseDownHandler(window, Element_Mousedown);
        TextBox removeTextbox = new TextBox();
        removeTextbox = (TextBox)sender;
        window.Children.Remove(removeTextbox);

    }

}

4

1 回答 1

1

为什么会如此复杂?下面的简单方法从指定位置的 Canvas 中删除任何子元素。

public static void RemoveChild(Canvas canvas, Point position)
{
    var element = canvas.InputHitTest(position) as UIElement;
    UIElement parent;

    while (element != null &&
        (parent = VisualTreeHelper.GetParent(element) as UIElement) != canvas)
    {
        element = parent;
    }

    if (element != null)
    {
        canvas.Children.Remove(element);
    }
}

PreviewMouseDown您可以在Canvas的处理程序中调用它:

private void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (eraseChildren)
    {
        var canvas = sender as Canvas;
        RemoveChild(canvas, e.GetPosition(canvas));
    }
}
于 2013-03-01T11:00:42.260 回答