1

following an answer from @sky-dev here i could implement a solution to the airspace problem in WPF when working with a WinFormsHost Control. But i'm new to WPF and now i'm trying to understand his code, and i don't see why some dependency properties are defined as attached properties and then their wrappers are implemented as for normal dependency properties.

I'll try to better explain my confusion with code:

First, the dependency properties are defined:

public class AirspacePopup : Popup
{
    public static readonly DependencyProperty IsTopmostProperty =
        DependencyProperty.Register("IsTopmost",
                                    typeof(bool),
                                    typeof(AirspacePopup),
                                    new FrameworkPropertyMetadata(false, OnIsTopmostChanged));

    public static readonly DependencyProperty FollowPlacementTargetProperty =
        DependencyProperty.RegisterAttached("FollowPlacementTarget",
                                            typeof(bool),
                                            typeof(AirspacePopup),
                                            new UIPropertyMetadata(false));

    public static readonly DependencyProperty AllowOutsideScreenPlacementProperty =
        DependencyProperty.RegisterAttached("AllowOutsideScreenPlacement",
                                            typeof(bool),
                                            typeof(AirspacePopup),
                                            new UIPropertyMetadata(false));

    public static readonly DependencyProperty ParentWindowProperty =
        DependencyProperty.RegisterAttached("ParentWindow",
                                            typeof(Window),
                                            typeof(AirspacePopup),
                                            new UIPropertyMetadata(null, ParentWindowPropertyChanged));

As you can see, the first DP is defined as a normal DP, as it is registered with the DependencyProperty.Register(..) method, but the rest of DP are defined as Attached, because they are registered with the DependencyProperty.RegisterAttached(..) method.

Now the wrappers:

public bool IsTopmost
    {
        get { return (bool)GetValue(IsTopmostProperty); }
        set { SetValue(IsTopmostProperty, value); }
    }
    public bool FollowPlacementTarget
    {
        get { return (bool)GetValue(FollowPlacementTargetProperty); }
        set { SetValue(FollowPlacementTargetProperty, value); }
    }
    public bool AllowOutsideScreenPlacement
    {
        get { return (bool)GetValue(AllowOutsideScreenPlacementProperty); }
        set { SetValue(AllowOutsideScreenPlacementProperty, value); }
    }
    public Window ParentWindow
    {
        get { return (Window)GetValue(ParentWindowProperty); }
        set { SetValue(ParentWindowProperty, value); }
    }

All the wrappers are implemented for normal DP... As far as i now, if a DP is registered as attached, the wrapper should be something like:

public static bool GetFollowPlacementTarget(UIElement element)
{
    if (element == null)    
    {        
        throw new ArgumentNullException(...);
    }
    return (bool)element.GetValue(FollowPlacementTarget); 
}

public static void SetFollowPlacementTarget(UIElement element, bool value) 
{    
    if (element == null)
    {
        throw new ArgumentNullException(...);
    }
    element.SetValue(FollowPlacementTarget, value);
} 

So what could be the reason to register a dependency property as an attached property when it hasn't a wrapper to use it as attached property at XAML? Could someone please explain why this code is implemented like that?

Thank you.

Community
  • 1
  • 1
MorgoZ
  • 2,012
  • 5
  • 27
  • 54

0 Answers0