How do I link the InkToolbar to the InkCanvas that is inside the CustomControl?

I am creating a CustomControl that contains InkCanvas

. Now the problem is how to bind InkToolbar

(which is outside the CustomControl) to InkCanvas

(which is inside the CustomControl)?

Decision:

I tried to get InkCanvas

outside of the CustomControl using the below code but doesn't work .

Here is my code (with a solution I tried that didn't work ):

//In CustomControl Code Behind
InkCanvas PATH_INK_CANVAS;

protected override void OnApplyTemplate()
{
    PATH_INK_CANVAS = GetTemplateChild<InkCanvas>("PATH_INK_CANVAS");
}

T GetTemplateChild<T>(string elementName) where T : DependencyObject
{
    var element = GetTemplateChild(elementName) as T;
    if (element == null)
        throw new NullReferenceException(elementName);
    return element;
}

public InkCanvas InkCanvas
{
    get { return PATH_INK_CANVAS; }
}

public static readonly DependencyProperty InkCanvasProperty =
    DependencyProperty.Register("InkCanvas", typeof(InkCanvas), typeof(RichInkTextBox), new PropertyMetadata(0));

//In CustomControl XAML
<Style>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid Name="MainGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                    <InkCanvas Name="PATH_INK_CANVAS" Canvas.ZIndex="-1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

//In Page
<local:CustomControl x:Name="MyCustomControl"/>
<InkToolbar Grid.Row="0" TargetInkCanvas="{x:Bind MyCustomControl.InkCanvas}"/>

      

+3


source to share


1 answer


I don't think the correct syntax is for defining a read-only dependency property. Try this instead:

public InkCanvas InkCanvas
{
    get => (InkCanvas)GetValue(InkCanvasProperty);
    private set => SetValue(InkCanvasProperty, value);
}
public static readonly DependencyProperty InkCanvasProperty = DependencyProperty.Register(
    "InkCanvas", typeof(InkCanvas), typeof(InkCanvasWrapper), new PropertyMetadata(null));

      



Also, make sure you set Mode

to x:Bind

in OneWay

as the default for the dependency property InkCanvas

is null

(you are setting the default 0

, which is wrong).

<InkToolbar Grid.Row="0" TargetInkCanvas="{x:Bind MyCustomControl.InkCanvas, Mode=OneWay}" />

      

0


source







All Articles