void line_KeyDown(object sender, KeyEventArgs e) { e.Handled = true; // 获取 textBox 对象的相对于 Canvas 的 x坐标 和 y坐标 double x = (double)line.GetValue(Canvas.LeftProperty); double y = (double)line.GetValue(Canvas.TopProperty); switch (e.Key) { case Key.Up: line.SetValue(Canvas.TopProperty, y - 1); break; case Key.Down: line.SetValue(Canvas.TopProperty, y + 1); break; case Key.Left: line.SetValue(Canvas.LeftProperty, x - 1); break; case Key.Right: line.SetValue(Canvas.LeftProperty, x + 1); break; case Key.Delete: Canvas c = line.Parent as Canvas; c.Children.Remove(line); break; case Key.C: break; case Key.A: break; case Key.V: if (Keyboard.Modifiers == ModifierKeys.Control) { BPLine tb = new BPLine(); tb.Cursor = Cursors.Hand; tb.SetValue(Canvas.LeftProperty, (double)line.GetValue(Canvas.LeftProperty) + 15); tb.SetValue(Canvas.TopProperty, (double)line.GetValue(Canvas.TopProperty) + 15); Canvas s1c = line.Parent as Canvas; try { s1c.Children.Add(tb); } catch { s1c.Children.Remove(tb); } } break; default: break; } }
protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); return; e.Handled = true; // 获取 textBox 对象的相对于 Canvas 的 x坐标 和 y坐标 double x = (double)this.GetValue(Canvas.LeftProperty); double y = (double)this.GetValue(Canvas.TopProperty); switch (e.Key) { case Key.Up: this.SetValue(Canvas.TopProperty, y - 1); this.MyLine.SetValue(Canvas.TopProperty, y - 1); break; case Key.Down: this.SetValue(Canvas.TopProperty, y + 1); this.MyLine.SetValue(Canvas.TopProperty, y + 1); break; case Key.Left: this.SetValue(Canvas.LeftProperty, x - 1); this.MyLine.SetValue(Canvas.LeftProperty, x - 1); break; case Key.Right: this.SetValue(Canvas.LeftProperty, x + 1); this.MyLine.SetValue(Canvas.LeftProperty, x + 1); break; case Key.Delete: Canvas c = this.Parent as Canvas; c.Children.Remove(this); break; case Key.C: break; case Key.A: break; case Key.V: if (Keyboard.Modifiers == ModifierKeys.Control) { BPLine tb = new BPLine(); tb.Cursor = Cursors.Hand; tb.SetValue(Canvas.LeftProperty, (double)this.GetValue(Canvas.LeftProperty) + 15); tb.SetValue(Canvas.TopProperty, (double)this.GetValue(Canvas.TopProperty) + 15); Canvas s1c = this.Parent as Canvas; try { s1c.Children.Add(tb); } catch { s1c.Children.Remove(tb); } } break; default: break; } base.OnKeyDown(e); }