WPF:自定义控件属性已被另一个自定义控件注册错误。

9
我创建了两个自定义控件,它们完全相同,只是类型不同。
ControlOne
public class ControlOne : TextEdit
    {
        public static readonly DependencyProperty AwesomeSauceProperty =
         DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(FormTextEditInput));           

        public string AwesomeSauce
        {
            get { return GetValue(AwesomeSauceProperty) as string; }
            set { SetValue(AwesomeSauceProperty, value); }
        }

    }

ControlTwo

public class ControlTwo : PasswordEdit
        {
            public static readonly DependencyProperty AwesomeSauceProperty =
             DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(FormTextEditInput));           

            public string AwesomeSauce
            {
                get { return GetValue(AwesomeSauceProperty) as string; }
                set { SetValue(AwesomeSauceProperty, value); }
            }

        }

而在XAML中,我只需这样做:

<controls:ControlOne AwesomeSauce="Yummy"/>
<controls:ControlTwo AwesomeSauce="Tummy"/>

我遇到了错误。

System.ArgumentException
'AwesomeSauce' property was already registered by 'ControlOne'.

您可能会问为什么我需要两个做同样事情的控件,我可以只创建数据模板并继续使用。但是我想要固执地说我需要不同类型的自定义控件做同样的事情。如果我可以使用通用类型的自定义控件将会很好,但我发现这是不可能的(对吗?)。
我也不想使用不同的名称,因为那只是一个解决问题的hack。
我只想让我的两个控件能够使用相同的名称作为它们的依赖属性。我错过了什么吗?或者说我完全不允许使用相同的名称?
我猜附加属性可能是解决方案,但我真的想推销一个自定义控件。
3个回答

22

DependencyProperty.Register方法的第三个参数ownerType必须是注册属性的类的类型,即在您的情况下是ControlOne和ControlTwo:

public class ControlOne : TextEdit
{
    public static readonly DependencyProperty AwesomeSauceProperty =
        DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(ControlOne));
    ...
}

public class ControlTwo : TextEdit
{
    public static readonly DependencyProperty AwesomeSauceProperty =
        DependencyProperty.Register("AwesomeSauce", typeof(string), typeof(ControlTwo));
    ...
}

2

除了被接受的答案适用于这种情况之外,在某些情况下,可能是因为您没有注册您的属性staticDependencyProperty并不总是依赖于DependencyObject,例如,您可能正在构建一个具有像DepObj.DepProp = "some prop"这样的属性的DependencyObject,但是DependencyObject尚未构建,因此DependencyProperty必须始终是static(而且由于编程逻辑,public static应该是readonly

DependencyProperty声明错误:

public DependencyProperty CProp = DependencyProperty.Register(
            "a",
            typeof(B),
            typeof(C),
            new PropertyMetadata(new C() { a= "default" }, CPropCallBack)
        );

依赖属性的正确声明:

public static readonly DependencyProperty CProp = DependencyProperty.Register(
            "a",
            typeof(B),
            typeof(C),
            new PropertyMetadata(new C() { a= "default" }, CPropCallBack)
        );

-1

我也遇到了同样的问题,但只有一个名为“Vista.xaml”的UserControl会在我的主窗口的多个选项卡页面中创建多次。

我发现属性名称、变量名称和应用程序中其他名称之间存在许多关系。

因此,我必须避免在xaml样式、代码后台、变量、函数等方面发生名称冲突。这解决了我的问题。

这是我“MainWindow.xaml”中的控件。

<local:Vista Margin="8,10,10,0" Titulo="Here goes title"/>

这是我的自定义控件“Vista”内部的标签,它显示了由属性“Titulo”设置的标题。

Codigo: 

  <Label x:Name="labelTitulo" Content="{Binding ElementName=ControlVista, 
  Path=Titulo}" Style="{StaticResource estiloTituloVista}" />

//////////

这是位于UserControl代码后台“Vista.xaml.cs”中的属性。

enter code here
       public static readonly DependencyProperty TituloProperty =
            DependencyProperty.Register
            (
                "Titulo",
                typeof(string),
                typeof(Vista),
                new FrameworkPropertyMetadata("")
            );
        public string Titulo
        {
            get { return (string)GetValue(TituloProperty); }
            set { SetValue(TituloProperty, value); }
        }

enter code here

这段代码应该放在UserControl主类之前。

  public Vista()
     {
       InitializeComponent();

       if (Titulo != null && Titulo != "") { labelTitulo.Content = Titulo; }
           
     }

为了将属性分配给其子项之一,您必须为UserControl命名。

在我的情况下,我将其命名为“ControlVista”

此名称与子标签控件“labelTitulo”的Content属性绑定

Vista.xaml设计模式的视图

在mainwindow.xaml中的外观如何


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接