我希望只将依赖属性附加到特定的控件上。
如果只是针对一种类型的控件,我可以这样做:
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty", typeof(object), typeof(ThisStaticWrapperClass));
public static object GetMyProperty(MyControl control)
{
if (control == null) { throw new ArgumentNullException("control"); }
return control.GetValue(MyPropertyProperty);
}
public static void SetMyProperty(MyControl control, object value)
{
if (control == null) { throw new ArgumentNullException("control"); }
control.SetValue(MyPropertyProperty, value);
}
(因此:限制在Get/Set方法中的Control
类型)
但现在我想要允许该属性附加到不同类型的Control
上。
你可以尝试为这个新类型添加两种方法的重载,但由于"Unknown build error, Ambiguous match found."而无法编译。
那么,我如何将我的DependencyProperty
限制为一组Control
?
(注意:在我特定的情况下,我需要它用于TextBox
和ComboBox
)
SetMyProperty(TextBox control, object value)
和SetMyProperty(ComboBox control, object value)
,是否会出现错误?如果是这样,那么这可能是附加属性系统的限制。如果是这样,除了将其附加到最大公共基类并在附加到不受支持的控件类型时引发异常之外,可能没有其他解决方法... - user1228