Remove Children from InkCanvas

I have an InkCanvas in a window in which I allow the user to draw with a stylus, touch, or mouse. I also allow the user to add text. The user clicks the "add text" button, then clicks where they would like to receive their text on the canvas. There appears a text box allowing them to print. When you enter or lose focus, create a ContentControl and add it to myInkCanvas.Children .

I want the user to be able to erase the text that they created when InkCanvas is in erase mode (I use EraseByPoint ). I tried to capture the MouseEnter and PreviewMouseMove events of the content controls, but they don't seem to fire.

Is there an event that I can capture? Is there a better way to handle this scenario? Is it possible?

+4
source share
1 answer

For this purpose you can use test testing.
look here

get InkPresenter first

 public T GetVisualChild<T>(Visual parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); child = v as T; if (child == null) { child = GetVisualChild<T>(v); } if (child != null) break; } return child; } InkPresenter inkPresenter = GetVisualChild<InkPresenter>(myInkCanvas); 

then get HitTestResult of your point

 HitTestResult hitTestResult = VisualTreeHelper.HitTest(inkPresenter, new Point(x, y)); 

then you can use hitTestResult.VisualHit to delete this object

+4
source

All Articles