Search code examples
c#wpfvisual-studiodependency-properties

WPF ICommand DependencyProperty Showing 'Binding' can only be set on a DependencyProperty of a DependencyObject in designer


I have a custom button control which has an ICommand property configured as a dependency property as follows:

public partial class SpecialButton : UserControl
{
public static readonly DependencyProperty s_buttonCommand =
            DependencyProperty.Register(nameof(ButtonCommand), typeof(ICommand), typeof(SpecialButton), new PropertyMetadata(null, _ButtonCommandChanged));

 public ICommand ButtonCommand
        {
            get { return (ICommand)GetValue(s_buttonCommand); }
            set { SetValue(s_buttonCommand, value); }
        }
}

Whenever I create an instance of the button in xaml, I get the following error in the designer in VisualStudio:

A 'Binding' cannot be set on the 'ButtonCommand' property of type 'Common_WPF_SpecialButton_12_635476180'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

It compiles fine when you actually run the code and it works perfectly but this error in the designer is annoying and I can't work out how to get rid of it. I've checked over the names and types in the dependency property registration and I don't think it's that. Also, the other properties on things like background colours that this control has all work fine and their dependency property registration looks exactly the same.

Any help much appreciated!


Solution

  • The documentation states that:

    The naming convention of the property and its backing DependencyProperty field is important. The name of the field is always the name of the property, with the suffix Property appended.

    You should rename s_buttonCommand to ButtonCommandProperty.