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

Adr*_*cia 5 wpf xaml wpf-controls

我创建了两个完全相同的自定义控件,只是它们属于两种不同的类型。

控制一

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

    }
Run Code Online (Sandbox Code Playgroud)

控制二

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

        }
Run Code Online (Sandbox Code Playgroud)

在 XAML 中,我只是这样做

<controls:ControlOne AwesomeSauce="Yummy"/>
<controls:ControlTwo AwesomeSauce="Tummy"/>
Run Code Online (Sandbox Code Playgroud)

我得到错误

System.ArgumentException
'AwesomeSauce' property was already registered by 'ControlOne'.
Run Code Online (Sandbox Code Playgroud)

你可能会问为什么我需要两个做同样事情的控件,我可以创建数据模板然后继续。但我想固执地说我需要不同类型的自定义控件来做同样的事情。如果我可以使用通用类型的自定义控件就好了,但我发现这是不可能的(对吧?)。

我也不想使用不同的名称,因为这只是解决问题的方法。

我只希望我的两个控件能够对其依赖属性使用相同的名称。有什么我在这里想念的吗?或者我只是完全不允许使用相同的名称?

我猜附加属性将是解决方案,但我真的想推动自定义控件。

Cle*_*ens 12

第三个参数ownerType中的DependencyProperty.Register方法必须是注册的财产,即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));
    ...
}
Run Code Online (Sandbox Code Playgroud)