Reputation: 2572
I am having trouble setting my ContentProperty to "Text". The error I am given is:
Invalid ContentPropertyAttribute on type 'MyType', property 'Text' not found.
The code behind looks like this:
[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);
}
}
}
I have actually got it to work if I name the CLR property something other than the DependencyProperty - am I using DependencyProperties incorrectly?
Upvotes: 1
Views: 749
Reputation: 19180
I thought it would be because typeof(LinkText) should be typeof(MyType), but I was able to get my test project to compile. Could you post the XAML file which is causing the error?
EDIT: Followup
Your problem is the two static methods you have in your code sample. Try removing those, and it should compile and work. The static methods only work with Attached Properties, not Dependency Properties.
Upvotes: 4
Reputation: 15403
The error is coming from you're default value, set in:
... new PropertyMetadata(false) ...
Because the TextProperty is of type string, it expects a string for the default value. Try:
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof (string),
typeof(MyType),
new PropertyMetadata(String.Empty));
Upvotes: 1