-3
캔버스에 많은 선이 있습니다. 난 단지하고 싶다 어떤 라인이 클릭과 클릭 WP30에서 아이들로부터 그것을 제거 ?? 어떤 해결책 감사합니다.캔버스에서 행을 캔버스에서 감지하고 WPF에서 제거하는 방법
캔버스에 많은 선이 있습니다. 난 단지하고 싶다 어떤 라인이 클릭과 클릭 WP30에서 아이들로부터 그것을 제거 ?? 어떤 해결책 감사합니다.캔버스에서 행을 캔버스에서 감지하고 WPF에서 제거하는 방법
먼저 캔버스에 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;
}
그런 다음 당신은 당신의 Delete
및 KeyDown
작업을 정의 할 수 있습니다. 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);
}
그래 덕분에 작품 :) :) 고맙네 @Anthony – Ahmad