Vahid Rassouli Vahid Rassouli - 3 months ago 47
C# Question

Implementing Hit Testing for Strokes of InkCanvas

I've implemented a custom InkCanvas and multiple custom Strokes, which are described here:
Custom Rendering Ink

Every thing works fine, except I can't do Hit Testing on my strokes.
At the "Implementing Custom Strokes" section of the above link, it says:

The Stroke class can also perform hit testing. You can also implement your own hit testing algorithm by overriding the HitTest method in the current class.

but i cant find any override-able version of HitTest method there!!!

Can anybody please help me implement hit testing on a custom stroke?



Well, found the solution.

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        if (EditingMode == InkCanvasEditingMode.Select)
            var strokes = Strokes.Where(x => (x is CustomStroke) && ((CustomStroke)x).HitTestPoint(e.GetPosition(this))).ToList();

            if (strokes.Any())
                Select(new StrokeCollection(strokes));