我无法将我的 ContentProperty 设置为“文本”。我得到的错误是:
“MyType”类型的 ContentPropertyAttribute 无效,找不到属性“Text”。
后面的代码是这样的:
[ContentProperty("Text")]
public partial class MyType: UserControl
{
public MyType()
{
InitializeComponent();
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
typeof (string),
typeof(MyType)));
public static string GetText(DependencyObject d)
{
return (string) d.GetValue(TextProperty);
}
public static void SetText(DependencyObject d, string value)
{
d.SetValue(TextProperty, value);
}
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
}
如果我将 CLR 属性命名为 DependencyProperty 以外的名称,我实际上已经可以正常工作了 - 我是否错误地使用了 DependencyProperties?
最佳答案
我认为这是因为 typeof(LinkText) 应该是 typeof(MyType),但我能够编译我的测试项目。您可以发布导致错误的 XAML 文件吗?
编辑:跟进
您的问题是代码示例中的两个静态方法。尝试删除那些,它应该可以编译和工作。静态方法仅适用于附加属性,不适用于依赖属性。
关于c# - 属性 'Text' 上的 ContentPropertyAttribute 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1042084/