Reputation: 811
I wrote a customcontrol inhert Control
and add a DependencyProperty content
which I want it to be the same as the content
of the button control.
Here is the code:
[System.ComponentModel.Bindable(true)]
public object Content
{
get { return (object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(object), typeof(SContent), null);
But after I reference the dll and run the program, WPF threw this error:
What's wrong with this? Would you please help me? Thank you.
Upvotes: 1
Views: 738
Reputation: 169340
If you want your custom control to support direct content in XAML, you should decorate it with the ContentPropertyAttribute :
[ContentProperty("Content")]
public class SContent : Control
{
[System.ComponentModel.Bindable(true)]
public object Content
{
get { return GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content), typeof(object), typeof(SContent), null);
}
But if you just want a Content
property you might as well inherit from ContentControl
instead of Control
. Then you also get a ContentTemplate
property and some other stuff "for free".
Upvotes: 1