gpt4 book ai didi

c# - 如何将 InkToolbar 链接到 CustomControl 内的 InkCanvas?

转载 作者:行者123 更新时间:2023-11-30 18:17:08 24 4
gpt4 key购买 nike

我正在创建一个包含 InkCanvas 的 CustomControl。现在的问题是如何将 InkToolbar(位于 CustomControl 外部)链接到 InkCanvas(位于 CustomControl 内部)?

尝试的解决方案:

我尝试使用以下代码在 CustomControl 之外获取 InkCanvas,但它不工作

这是我的代码(我试过的解决方案不起作用):

//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}"/>

最佳答案

我认为这不是定义只读 依赖属性的正确语法。试试下面这样的东西 -

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));

此外,确保将 x:BindMode 设置为 OneWay 作为 InkCanvas< 的默认值 依赖属性为 null(您将默认值设置为 0,这是错误的)。

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

关于c# - 如何将 InkToolbar 链接到 CustomControl 内的 InkCanvas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com