代码之家  ›  专栏  ›  技术社区  ›  CCarter

只能在设计器中DependencyObject的DependencyProperty上设置显示“Binding”的WPF ICommand DependencyProperties[重复]

  •  0
  • CCarter  · 技术社区  · 1 年前

    我有一个自定义按钮控件,它的ICommand属性配置为依赖属性,如下所示:

    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); }
            }
    }
    

    每当我在xaml中创建按钮的实例时,我都会在VisualStudio中的设计器中得到以下错误:

    无法在“Common_WPF_SpecialButton_12_635476180”类型的“ButtonCommand”属性上设置“Binding”。只能在DependencyObject的DependencyProperty上设置“Binding”。

    当你真正运行代码时,它编译得很好,而且工作得很完美,但设计器中的这个错误很烦人,我不知道如何消除它。我已经检查了依赖属性注册中的名称和类型,我不认为是这样。此外,该控件具有的背景颜色等其他属性都可以正常工作,并且它们的依赖属性注册看起来完全相同。

    非常感谢您的帮助!

    1 回复  |  直到 1 年前
        1
  •  3
  •   Orace    1 年前

    文件 states 那个

    属性及其支持的DependencyProperty字段的命名约定非常重要。字段的名称始终是属性的名称,并附加后缀property。

    您应该重命名 s_buttonCommand ButtonCommandProperty .