0

I change the layout dynamically at runtime.
There was a task to rewrite the Attached Property values from the element being removed to the embedding element.
I get all the assigned properties using the GetLocalValueEnumerator() method.
But now I need to somehow select only the Attached property from the general DependecyProperty list.

I read a good explanation about the differences between them here: What's the difference between a dependency property and an attached property in WPF?
But I did not understand how to use this information for my purpose.

Updating

According to the comments from @Clemens, I created such a method. So far it works acceptable for my solution. Perhaps in the future there will be some nuances. And then I will think about how to eliminate them.

        private static bool IsDependecyProperty(Type ownerTYpe, DependencyProperty property)
        {
            if (typeof(DependencyObject).IsAssignableFrom(property.OwnerType) &&
                property.OwnerType.IsAssignableFrom(ownerTYpe))
            {
                string propName = property.Name;
                return ownerTYpe.GetProperty(propName) != null;
            }
            return false;
        }
EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • 1
    The `DependencyProperty` class' `Register` and `RegisterAttached` functions both create the same type of object (the guy in your link acknowledges this). The difference is mostly in the rules; that is , what is can be done with the property at *runtime*. How can it be bound? Think of these functions as each making an entry in a global table and returning you a key to that entry. Each property entry dictates the rules that WPF subsequently obeys. For example, can it be applied to another object or not? Can it be inherited or not? etc. – Joe Jan 26 '22 at 22:59
  • 1
    You may perhaps iterate through all PropertyInfos return by Type.GetProperties of the element in question, and remove all dependency properties for which a CLR property wrapper exists from your list. All remaining dependency properties should have been registered as attached properties. – Clemens Jan 26 '22 at 23:09
  • @Clemens, I've updated my question with the solution code created according to your comment. Can you guess in what situations it might not work correctly? – EldHasp Jan 27 '22 at 10:40
  • 1
    Could be written as `return property.OwnerType.IsAssignableFrom(ownerTYpe) && ownerType.GetProperty(property.Name) != null;` – Clemens Jan 27 '22 at 10:45
  • @Clemens, I do not argue. For debugging, it is more convenient for me with the if block. And logically - can we expect some surprises? – EldHasp Jan 27 '22 at 11:46
  • 1
    Sure you can. It is always possible that a class declares an attached property and besides the static get/set methods also exposes a wrapper property. – Clemens Jan 27 '22 at 11:50

0 Answers0