I have some basic code that, when a control is dropped on a Canvas, I need the user to be able to delete said control via a simple key press.
private void PlaceElementOnCavas(UIElement element, Point point) {
Canvas.SetLeft(element, point.X);
Canvas.SetTop(element, point.Y);
// Add the event to allow the user to press delete and remove the control
element.KeyDown += (sender, e) => {
if (e.Key == Key.Delete) {
this.designCanvas.Children.Remove(element);
}
};
this.designCanvas.Children.Add(element);
}
My code looks like that. My control is added fine, at exactly the point on the Canvas I need it do.
The event handler does nothing, whether I try to add it via a lambda or via a traditional call to another method.
What am I missing?