WinUI 3 自定义控件属性绑定不起作用

问题描述 投票:0回答:1

我创建了一个自定义控件,该控件应该用作项目视图中的按钮,但是,更改其文本的绑定不起作用。

CustomControls(Winui3类库项目)/CustomTableItem.cs

namespace CustomControls
{
    public sealed class CustomTableItem : Control
    {

        DependencyProperty TableNameProperty = DependencyProperty.Register(
            nameof(TableName),
            typeof(string),
            typeof(CustomTableItem),
            new PropertyMetadata(null));

        public string TableName
        {
            get => (string)GetValue(TableNameProperty);
            set => SetValue(TableNameProperty, value);
        }

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

        public CustomTableItem(string TableName)
        {
            this.DefaultStyleKey = typeof(CustomTableItem);
            this.TableName = TableName;
        }
    }
}

自定义控件/主题/Generic.xaml

//one other style a
c# xaml custom-controls winui-3 winui
1个回答
1
投票

您应该使用 Live Visual Tree 来调试此类问题。您可以看到每个控件的属性

Live Visual Tree Button

我猜你看不到它,因为

Foreground
白色。右键单击
TextBlock
并选择 显示属性

Show Properties Menu

尝试使用

TemplateBinding
:

<TextBlock Text={TemplateBinding TableName} .../>

我还发现您的依赖属性中缺少

public static readonly
doc

public static readonly DependencyProperty TableNameProperty =
    DependencyProperty.Register(
        nameof(TableName),
        typeof(string),
        typeof(CustomTableItem),
        new PropertyMetadata(null));
© www.soinside.com 2019 - 2024. All rights reserved.