Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It will end up in a method looking like this</p> <pre><code>public class DockPanel : Panel { public static readonly DependencyProperty DockProperty; // ... public static void SetDock(UIElement element, Dock dock) { element.SetValue(DockProperty, value); } } </code></pre> <p>As you can see, it's actually not set on the parent, but the CheckBox itself, through the static method SetDock on DockPanel and not the parent instance. Doing it in code behind makes this a little clearer, notice how we never use an instance of a DockPanel.</p> <pre><code>DockPanel.SetDock(checkBox1, Dock.Top); DockPanel.SetDock(checkBox2, Dock.Bottom); </code></pre> <p>Hopefully this was clear, unless your question was how this works "under the hood". In that case, see <a href="https://stackoverflow.com/questions/1173494/how-exactly-do-attached-properties-work-in-wpf">this</a> question.</p> <p>Quote from link. </p> <blockquote> <p>The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves.</p> </blockquote> <p>A CheckBox has no use for a Dock property unless it is in a DockPanel. Same goes for Grid.Row, Canvas.Left, Validation.HasError (read only) etc. So basically, the DockPanel is the one needing the information, but it needs all its childs to be able to store it. Hence, it's using an Attached Property for it. If you created a new Panel, called PuneetPanel, and you needed an Angel to calculate the child position, then you could define your own Attached Property, PuneetPanel.Angel inside this panel and all childs could use this without having to be subclassed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload