2017-04-15 6 views

답변

1

먼저 캔버스에 MouseLeftButtonDown 이벤트를 추가하고 WPF에 KeyDown 이벤트를 추가해야합니다.

public MainWindow() 
    { 
     InitializeComponent(); 
     MyCanvas.MouseLeftButtonDown += MyCanvas_MouseLeftButtonDown; 
     this.KeyDown += MainWindow_KeyDown; 
    } 

마우스 왼쪽 버튼을 클릭하면 선택 항목이 강조 표시됩니다. 다른 항목을 클릭하면 이전 선택을 강조 표시해야합니다.

private Line _selectedLine; 
    private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     object testPanelOrUi = InputHitTest(e.GetPosition(this)) as FrameworkElement; 

     // if the selection equals _selectedLine, i.e. the line has been selected already 
     if (Equals(testPanelOrUi, _selectedLine)) return; 

     // The selection is different. 
     // if _selectedLine is not null, revert color change. 
     if (_selectedLine != null) 
     { 
      UnHighlightSelection(); 
     } 

     // if testPanelOrUi is not a line. 
     if (!(testPanelOrUi is Line)) return; 

     // The selection is different and is a line. 
     _selectedLine = (Line) testPanelOrUi; 
     HighlightSelection(_selectedLine); 
    } 

귀하의 HighlightSelection()UnHighlightSelection()은 다음과 유사 할 수 :

private void HighlightSelection(Line selectedob) 
    { 
     selectedob.Stroke = Brushes.Red; 
    } 

    private void UnHighlightSelection() 
    { 
     //if nothing has been selected yet. 
     if (_selectedLine == null) return; 

     _selectedLine.Stroke = Brushes.Black; 
     _selectedLine = null; 
    } 

그런 다음 당신은 당신의 DeleteKeyDown 작업을 정의 할 수 있습니다. Delete 키를 누르면 선택 항목이 삭제됩니다.

private void MainWindow_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Delete) 
     { 
      DeleteLine(); 
     } 
    } 

    public void DeleteLine() 
    { 
     //if nothing has been selected yet. 
     if (_selectedLine == null) return; 

     //if the selection has been deleted. 
     if (!MyCanvas.Children.Contains(_selectedLine)) return; 

     UnHighlightSelection(); 
     MyCanvas.Children.Remove(_selectedLine); 
    } 
+0

그래 덕분에 작품 :) :) 고맙네 @Anthony – Ahmad

관련 문제