我创建了一个自定义控件,该控件应该用作项目视图中的按钮,但是,更改其文本的绑定不起作用。
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
您应该使用 Live Visual Tree 来调试此类问题。您可以看到每个控件的属性
我猜你看不到它,因为
Foreground
是白色。右键单击 TextBlock
并选择 显示属性。
尝试使用
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));