InkCanvas MVVM binding in UWP

764 views Asked by At

I have a list with InkStrokeContainer in a ListView and I try to bind the InkCanvas to it. In WPF the InkCanvas had a Strokes attribut but in UWP its not available. Another idea was to bind to an InkPresenter but how?

Then my idea was to create a InkCanvasControl which extends from InkCanvas and create the Property for the Strokes.

public InkStrokeContainer Strokes
{
    get
    {
        return this.InkPresenter.StrokeContainer;
    }
    set
    {
        this.InkPresenter.StrokeContainer = value;
    }
}

But with this I got the error: "Cannot assign property Strokes"... I used it this way:

<controls:InkCanvasControl Strokes="{Binding Strokes}"></controls:InkCanvasControl>

And the binding is from my ViewModel.

1

There are 1 answers

0
Jay Zuo On

The problem here is that the Strokes property you've created is not a dependency property, so it won't support binding and that's why you got the error. To solve this problem, you can register Strokes property as a dependency property like following:

public InkStrokeContainer Strokes
{
    get { return (InkStrokeContainer)GetValue(StrokesProperty); }
    set { SetValue(StrokesProperty, value); }
}

public static readonly DependencyProperty StrokesProperty = DependencyProperty.Register("Strokes",
    typeof(InkStrokeContainer), typeof(InkCanvasControl),
    new PropertyMetadata(null, new PropertyChangedCallback(OnLabelChanged)));

private static void OnLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (d as InkCanvasControl).InkPresenter.StrokeContainer = (InkStrokeContainer)e.NewValue;
}

For more info, please see Custom dependency properties.