What is a read-only attached property?

An attached property is a concept defined by Extensible Application Markup Language (XAML). An attached property is intended to be used as a type of global property that is settable on any object. In Windows Presentation Foundation (WPF), attached properties are typically defined as a specialized form of dependency property that does not have the conventional property "wrapper".

One purpose of an attached property is to allow different child elements to specify unique values for a property that is actually defined in a parent element. A specific application of this scenario is having child elements inform the parent element of how they are to be presented in the user interface (UI). One example is the DockPanel.Dock property. The DockPanel.Dock property is created as an attached property because it is designed to be set on elements that are contained within a DockPanel, rather than on DockPanel itself. The DockPanel class defines the static DependencyProperty field named DockProperty, and then provides the GetDock and SetDock methods as public accessors for the attached property.

The following example shows how you can set an attached property in code. In this example, myCheckBox is an instance of the CheckBox class

DockPanel myDockPanel = new DockPanel();

CheckBox myCheckBox = new CheckBox();

myCheckBox.Content = "Hello";

myDockPanel.Children.Add(myCheckBox);

DockPanel.SetDock(myCheckBox, Dock.Top);

 

The following example shows how to register an attached property as a dependency property

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(

  "IsBubbleSource",

  typeof(Boolean),

  typeof(AquariumObject),

  new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)

);

public static void SetIsBubbleSource(UIElement element, Boolean value)

{

  element.SetValue(IsBubbleSourceProperty, value);

}

public static Boolean GetIsBubbleSource(UIElement element)

{

  return (Boolean)element.GetValue(IsBubbleSourceProperty);

}

 

 

References:

1.       Attached Properties Overview

2.       How to: Register an Attached Property