2015-01-13 10 views
0

마우스 클릭시 캔버스에 노드 (타원)를 그릴 수있는 방법이 있습니다. 그런 다음 해당 노드 중 하나를 클릭하면 해당 노드의 x, y를 기반으로 계산됩니다 코드에 넣은 파일 경로의 이미지를 저장합니다. 다른 노드를 클릭하면 이미지의 위치가 새 노드의 x, y를 기준으로 업데이트됩니다. 여기에 나열된 내용을 시도했지만 Draw rectangle and update it on every mouse click, 마지막 이미지가 제거되지 않습니다. canvas1.Children.Remove이 (ROV)이 새로운 ROV가 캔버스에 배치되어 어디서든 전에 작동하지 않습니다 호출버튼 클릭시 캔버스에서 이미지 위치 업데이트

private void FindROVButton_Click(object sender, RoutedEventArgs e) 
    { 

     if (SelectedNode != null) // SelectedNode is the currently clicked node 
     { 
      if (System.Windows.MessageBox.Show("Calculate ROV location from \"" + SelectedNode.Name + "\"?", "Calculate ROV Location", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes) 
      { 

       Point rovPosition = new Point(); 
       rovPosition = calculateROV_position(SelectedNode.X, SelectedNode.Y, SelectedNode.Range, SelectedNode.Bearing); 


       Image rov = new Image(); 
       rov.Source = new BitmapImage(new Uri("C:\\Pic\\Example.png")); 
       rov.Width = 30; 
       rov.Height = 30; 

       Canvas.SetLeft(rov, (rovPosition.X - (rov.Width/2.0))); 
       Canvas.SetTop(rov, (rovPosition.Y - (rov.Height/2.0))); 

       canvas1.Children.Add(rov); 
      } 
     } 
    } 

-

여기 내이 시작 버튼 클릭에 대한 방법입니다. 캔버스에 항상 새로운 것을 추가합니다. 나는 그것이 선택된 노드를 기반으로한다는 사실과 관련이 있다고 생각합니다.

답변

0

알아 냈어. 메서드를 만들기 전에 먼저 새 이미지를 만든 다음 canvas1.Children.Add (rov) 메서드 앞에 canvas1.Children.Remove (rov) 메서드를 추가해야했습니다.

Image rov = new Image(); 
private void FindROVButton_Click(object sender, RoutedEventArgs e) 
{ 

    if (SelectedNode != null) 
    { 
     if (System.Windows.MessageBox.Show("Calculate ROV location from \"" + SelectedNode.Name + "\"?", "Calculate ROV Location", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes) 
      { 

       Point rovPosition = new Point(); 
       rovPosition = calculateROV_position(SelectedNode.X, SelectedNode.Y, SelectedNode.Range, SelectedNode.Bearing); 

       rov.Source = new BitmapImage(new Uri("D:\\Dev\\trunk\\Software\\Nav Assist\\Data\\Hull Cleaner Tiny Image.png")); 
       canvas1.Children.Remove(rov); 

       rov.Width = 30; 
       rov.Height = 30; 

       Canvas.SetLeft(rov, (rovPosition.X - (rov.Width/2.0))); 
       Canvas.SetTop(rov, (rovPosition.Y - (rov.Height/2.0))); 

       canvas1.Children.Add(rov); 
      } 
     } 
    } 

위의 코드는 정상적으로 작동합니다.

관련 문제