0

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?

user884248
  • 2,134
  • 3
  • 32
  • 57
  • @Clemens - my question was not if I need to register an attached property to the specific class, but if there is any way, except AddOwner, to apply an attached property to multiple classes at once. I understand that the anser is no? – user884248 Oct 04 '17 at 09:14
  • 1
    An attached property can by definition be applied to an instance of any class derived from DependencyObject. If you want to declare it as actual CLR property in different classes, you should use AddOwner, e.g. as shown here: https://stackoverflow.com/a/46505050/1136211 – Clemens Oct 04 '17 at 09:19

0 Answers0