You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinUI 3中创建含其他自定义控件的CustomControl及属性暴露问题

问题描述

我是WinUI和XAML新手,正在创建一个包含各类CustomControl的WinUI 3 Library。这些控件均可独立使用,但其中一个CustomContainer控件嵌入了库内的其他自定义控件。

容器控件的XAML代码

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyCustomControls">
    
    <Style TargetType="local:CustomContainer">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomContainer">
                    <StackPanel>
                        <local:CustomTextBlock x:Name="Text"
                                            DisplayMode="{TemplateBinding DisplayMode}"
                                            Message="{TemplateBinding Message}">
                        </local:CustomTextBlock>

                        <local:CustomIndicator x:Name="Indicator"></local:CustomIndicator>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>

如示例所示,CustomTextBlock自定义控件包含DisplayMode和Message两个DependencyProperty,我需要在CustomContainer上"复制"这些属性,以便在页面使用CustomContainer时能设置它们。我已在XAML中使用TemplateBinding,需要在CustomContainer的代码后置中声明这两个DependencyProperty。

CustomTextBlock控件的代码后置

private static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(
    nameof(DisplayMode), typeof(bool), typeof(CustomTextBlock), new PropertyMetadata(null));

public bool DisplayMode
{
    get => (bool)GetValue(DisplayModeProperty);
    set => SetValue(DisplayModeProperty, value);
}

private static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
    nameof(Message), typeof(string), typeof(CustomTextBlock), new PropertyMetadata(default(string), (d, e) => ((CustomTextBlock)d).MessageChanged(d, e)));

public string Message
{
    get => (string)GetValue(MessageProperty);
    set => SetValue(MessageProperty, value);
}

请问如何在CustomContainer控件上暴露CustomTextBlock的这两个属性,使设置的值直接作用于底层属性?它们仍需为DependencyProperty类型吗?这类似包装或继承概念,但我无法理清,尤其是带有事件处理程序的Message属性。


解决方案

1. 必须声明为DependencyProperty

是的,这些属性必须是DependencyProperty。只有依赖属性才能支持XAML的核心特性:数据绑定、样式设置、模板绑定、动画等。普通CLR属性无法和TemplateBinding配合工作,也无法在外部页面中通过绑定设置值。

2. 实现DisplayMode属性

在CustomContainer的代码后置中,按标准方式注册DisplayMode,注意bool是值类型,不能设为null,建议和CustomTextBlock保持一致的默认值(比如false):

public sealed class CustomContainer : Control
{
    // DisplayMode依赖属性
    public static readonly DependencyProperty DisplayModeProperty =
        DependencyProperty.Register(nameof(DisplayMode), typeof(bool), typeof(CustomContainer), new PropertyMetadata(false));

    public bool DisplayMode
    {
        get => (bool)GetValue(DisplayModeProperty);
        set => SetValue(DisplayModeProperty, value);
    }

3. 实现Message属性

同样注册Message依赖属性,由于已经在XAML中使用TemplateBinding,当CustomContainer的Message值变化时,会自动同步到内部的CustomTextBlock,而CustomTextBlock自身的MessageChanged回调会正常触发,不需要在容器层额外处理:

// Message依赖属性
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register(nameof(Message), typeof(string), typeof(CustomContainer), new PropertyMetadata(default(string)));

    public string Message
    {
        get => (string)GetValue(MessageProperty);
        set => SetValue(MessageProperty, value);
    }

    public CustomContainer()
    {
        this.DefaultStyleKey = typeof(CustomContainer);
    }
}

4. 关键说明

  • TemplateBinding的作用:它建立了容器属性和内部控件属性的单向绑定,外部设置CustomContainer的属性值时,会自动同步到内部的CustomTextBlock。
  • 属性变更回调:如果需要CustomContainer自身监听Message的变化,可以在注册依赖属性时添加自定义回调,但这不是必须的——内部CustomTextBlock的MessageChanged会因属性值同步自动执行。
  • 默认值一致性:建议容器属性的默认值和内部控件保持一致,避免出现预期外的初始状态差异。

内容的提问来源于stack exchange,提问作者Cheshire Cat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 05:45:33