c# - 属性 'Text' 上的 ContentPropertyAttribute 无效

标签 c# .net wpf

我无法将我的 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/

相关文章:

c# - 非严格的多接口(interface)类型参数约束?

c# - 使用 C# DateTime 将 SQL 日期时间设置为最小值

.net - 将 1.1 框架升级到 4.0

c# - 您可以在选择项目之前在 linq 查询中设置属性吗?

wpf - wpf自定义控件库和wpf类库有什么区别

c# - DataGrid 列标题与数据不对齐

wpf - 与 TextBlock 具有相同剪辑的 TextBox(TextBox 模板)

c# - 加快 StreamReader 的搜索/读取速度

c# - 如何清除绑定(bind)源?

java - 从 .NET 的角度来看,我需要了解不同版本的 Java 的哪些信息?