2017-08-13 67 views
0

我嘗試了WPF,我希望能夠通過左擊創建形狀,但有正確的單擊右鍵刪除鼠標指針當前盤旋的形狀,但是,會發生什麼反而是創建的最後一個形狀被刪除。我該如何解決?從WPF中刪除某些形狀

這將創建形狀:

private List<Shape> shapes = new List<Shape>(); 
    private Shape shape; 
    public static Random rand = new Random(); 

    private Rectangle CreateRectangle() 
    { 

     int height = rand.Next(0, 151); 
     int width = rand.Next(0, 101); 
     byte alpha = (byte)rand.Next(0, 256); 
     byte alpha2 = (byte)rand.Next(0, 256); 
     byte alpha3 = (byte)rand.Next(0, 256); 
     Rectangle rect = new Rectangle(); 
     rect.Width = width; 
     rect.Height = height; 
     SolidColorBrush color = new SolidColorBrush(); 
     color.Color = Color.FromRgb(alpha, alpha2, alpha3); 
     rect.Fill = color; 
     return rect; 
    } 

    private Ellipse CreateEllipse() 
    { 

     int height = rand.Next(0, 151); 
     int width = rand.Next(0, 101); 
     byte alpha = (byte)rand.Next(0, 256); 
     byte alpha2 = (byte)rand.Next(0, 256); 
     byte alpha3 = (byte)rand.Next(0, 256); 
     Ellipse ellipse = new Ellipse(); 
     ellipse.Width = width; 
     ellipse.Height = height; 
     SolidColorBrush color = new SolidColorBrush(); 
     color.Color = Color.FromRgb(alpha, alpha2, alpha3); 
     ellipse.Fill = color; 
     return ellipse; 

    } 

    private void ColumnDefinition_OnClick(object sender, MouseButtonEventArgs e) 
    { 
     Point area = System.Windows.Input.Mouse.GetPosition(mc); 
     int num = rand.Next(1, 3); 
     switch (num) 
     { 
      case 1: 
       shape = CreateRectangle(); 

       mc.Children.Add(shape); 
       shapes.Add(shape); 
       Canvas.SetLeft(shape, area.X); 
       Canvas.SetTop(shape, area.Y); 
       break; 
      case 2: 
       shape = CreateEllipse(); 
       mc.Children.Add(shape); 
       shapes.Add(shape); 
       Canvas.SetLeft(shape, area.X); 
       Canvas.SetTop(shape, area.Y); 
       break; 
     } 
    } 

    private void Clear_Click(object sender, RoutedEventArgs e) 
    { 
     mc.Children.Clear(); 
    } 

這是什麼應該刪除形狀:

private void mc_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 

     mc.Children.Remove(shape); 
     shapes.Remove(shape); 
    } 
} 

}

任何幫助是極大的讚賞。

回答

0

我不確定我在代碼示例中有我需要的所有東西,但它看起來像一個形狀正在創建並添加到列表中,但也被分配給名爲「shape」的成員,該成員被創造了最新的形狀。然後你總是去除那個最新的形狀。您應該將mousedown處理程序附加到每個形狀,從對象發件人捕獲形狀,並將其從列表中刪除。

private void shape_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    Shape s = sender as Shape; 
    mc.Children.Remove(s); 
    shapes.Remove(s); 
} 
0

我建議你學習WPF中的命中測試。看看this問題,我回答瞭如何在鼠標下獲取幾何並將其刪除。你只需要適應你的任務代碼,但它是非常容易的。

XAML:

<Canvas Mouse.MouseDown="Canvas_MouseDown"> 

後面的代碼:

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    var canvas = sender as Canvas; 
    if (canvas == null) 
     return; 

    HitTestResult hitTestResult = VisualTreeHelper.HitTest(canvas, e.GetPosition(canvas)); 
    var shape = hitTestResult.VisualHit as Shape; 
    if (shape == null) 
     return; 

    canvas.Children.Remove(shape); 
    shapes.Remove(shape); // I think you don't need list of shapes 
} 
0

MouseButtonEventArgs.OriginalSource屬性返回點擊Shape所以你可以實現你的mc_MouseRightButtonDown事件處理程序是這樣的:

private void mc_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    Shape clickedShape = e.OriginalSource as Shape; 
    if (clickedShape != null) 
    { 
     mc.Children.Remove(clickedShape); 
     shapes.Remove(clickedShape); 
    } 
}