Search code examples
c#.netwpfeventsinkcanvas

Is there a generic InkCanvas StrokesChanged type of event?


Essentially what I'm trying to do is trigger a sort of 'dirty' state for my canvas, so that I know if there are unsaved changes.

Is there an event in the WPF InkCanvas that I can use to handle any time when the strokes change?

If there isn't, what events should I listen to for the equivalent? My first guess would be these:

StrokeCollected
StrokeErased
StrokesReplaced

Though I could be wrong, and be missing an edge case.

Note: It's not a big deal if I get false positives (marked dirty when it is in fact not), but I don't want false negatives.


Solution

  • It seems like these events will do the job:

    • InkCanvas.StrokesReplaced (occurs when the Strokes property is set)
    • StrokeCollection.StrokesChanged (occurs when a stroke is added or removed)
    • Stroke.StylusPointsChanged (occurs when the shape of a stroke is changed)
    • Stroke.StylusPointsReplaced (occurs when the StylusPoints property is set)
    • Stroke.DrawingAttributesChanged (occurs when the attributes of a stroke are changed)
    • Stroke.DrawingAttributesReplaced (occurs when the DrawingAttributes property is set)

    In my case, I never replace properties, or change drawing attributes, so I only use StrokeCollection.StrokesChanged and Stroke.StylusPointsChanged. Here is a snippet of my code.

    public MainWindow()
    {
        inkCanvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
    }
    
    private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
    {
        // Mark dirty
        foreach (Stroke stroke in e.Added)
        {
            stroke.StylusPointsChanged += stroke_StylusPointsChanged;
        }
        foreach (Stroke stroke in e.Removed)
        {
            stroke.StylusPointsChanged -= stroke_StylusPointsChanged;
        }
    }
    
    private void stroke_StylusPointsChanged(object sender, System.EventArgs e)
    {
        // Mark dirty
    }