In a WPF application, I have defined the PropertyAnalysisTemplate attached property, which I want to be able to use for several types of controls.
I defined it like so:
public static readonly DependencyProperty PropertyAnalysisTemplateProperty = DependencyProperty.RegisterAttached(
"PropertyAnalysisTemplate", typeof(DataTemplate), typeof(FrameworkElement), new PropertyMetadata(
default(DataTemplate))
);
public static DataTemplate GetPropertyAnalysisTemplate(DependencyObject obj)
{
return (DataTemplate) obj.GetValue(PropertyAnalysisTemplateProperty);
}
public static void SetPropertyAnalysisTemplate(DependencyObject obj, DataTemplate value)
{
obj.SetValue(PropertyAnalysisTemplateProperty, value);
}
And everything works fine, but in the designer view I get the error: 'PropertyAnalysisTemplateProperty' was already registered by 'FrameworkElement'.
I suppose I need to register it for the specific element I use it for (TextBox in this specific case), and add other owners using AddOwners.
But honestly that looks like bad design to me. Isn't there a more object-oriented way of registering an attached property to a base class and all of its descendants?