using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace WinUI3CustomControlTest;
public sealed class InnerControl : Control
{
public static readonly DependencyProperty InnerTextProperty =
DependencyProperty.Register(
nameof(InnerText),
typeof(string),
typeof(InnerControl),
new PropertyMetadata(string.Empty));
public InnerControl()
{
this.DefaultStyleKey = typeof(InnerControl);
}
public string InnerText
{
get => (string)GetValue(InnerTextProperty);
set => SetValue(InnerTextProperty, value);
}
}
外部控件. cs**
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace WinUI3CustomControlTest;
[TemplatePart(Name = nameof(MyInnerControl), Type = typeof(InnerControl))]
public sealed class OuterControl : Control
{
public OuterControl()
{
this.DefaultStyleKey = typeof(OuterControl);
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
MyInnerControl = GetTemplateChild(nameof(MyInnerControl)) as InnerControl;
}
public InnerControl? MyInnerControl { get; private set;}
}
1条答案
按热度按时间enxuqcxy1#
一种方法是将内部控件存储在公共属性中。
代码如下: