Search code examples
wpfpaneldependency-propertiesuielementmeasureoverride

WPF - How to get my Panel to see if a DependencyProperty is set on my child when they are UIElements?


Basically, during my MeasureOverride I want to check to see if my child element has a certain property set on it regardless of what type of item it is.

public override Size MeasureOverride(Size availableSize)
{
    foreach (UIElement child in InternalChildren)
    {
        //Check for IsSelected property?
    }
}

How would one do this in a way to ensure that the child has the property available and then get the value for it? The problem is that UIElement does not have a IsSelected property and the panel can accept any child that supports that property, so I can't just cast to a specific type...


Well, I'm looking at 2 possible scenarios:

1) The child did not add itself as an owner of the Selector.IsSelected DependencyProperty, in which case that child is ignored entirely. 2) The child did add itself as an owner of the Selector.IsSelected DependencyProperty, in which case I want the that value.

Basically, I want the IsSelected value while also preventing my app from crashing in case there is no value associated to that child element.


Solution

  • Are you looking for the value of the Selector.IsSelected attached property? If so the following can help:

    bool isSelected = (bool)child.GetValue(Selector.IsSelectedProperty);