WPF在一个对象中创建标签和路径

时间:2014-12-05 22:44:42

标签: c# wpf

我尝试使用Label和Path创建一个类作为单个对象 所以我这样做了:

class MyLine : Shape
{
    Point p1,p2;
    Label lbl;
    Path arrow;

    public void AddToCanvas(Canvas x)
    {
        x.Children.Insert(0, lbl);
        x.Children.Insert(0, arrow);

        updateLBL(); 
        updateArrow();
    }
    public void RemoveFromCanvas(Canvas x)
    {
        x.Children.Remove(lbl);
        x.Children.Remove(arrow);
    }

    // ... etc
}

它工作正常,但我无法从画布中删除MyLine作为单个对象,我必须删除Path然后Label。

class MainClass
{
    // ... code

    public void DrawMyLine(MyLine m)
    {
        _Canvas.Children.Add(m);  // this work fine
    }

    object selectdeobject;

    public void CanvasMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
       selectdeobject = _Canvas.InputHitTest(e.GetPosition(_Canvas)) as FrameworkElement;
    }

    public void Delete()
    {
       if (selectdeobject != null)
       {
           if (selectdeobject is MyLine) // this always false
           {
                MyLine t = selectdeobject as MyLine;
                t.RemoveFromCanvas(_Canvas);
           }
           else if(selectdeobject is Path)
           {
                _Canvas.Children.Remove((selectdeobject as Path)) // this delete only the Path and keep the Label
           }
           else if(selectdeobject is Label)
           {
                _Canvas.Children.Remove((selectdeobject as Label)) // this delete only the Label and keep the Path
           }
        }
    }
}

使所选对象像单个对象一样的任何帮助吗?

0 个答案:

没有答案