title: How to use the Child-Defined Attached Properties?
body: <hr>
  
  <h2>1. <strong>Issue</strong></h2>
  
  <p>As we know,Attached Property widely expands the property system in wpf.But all the examples which are familiar to us are almost <strong>Parent-Defined</strong> ones,such as <strong>DockPanel.Dock / Grid.Row</strong> and so on.But after checking the document in MSDN,I found there are some other usages of Attached Property:</p>
  
  <blockquote>
    <p>From MSDN:<a href="http://msdn.microsoft.com/en-us/library/ms749011%28v=vs.110%29.aspx#howused">Attached Properties Overview / How Attached Properties Are Used by the Owning Type
    </a> </p>
    
    <p>1.The type that defines the attached property is designed so that it can be the parent element of the elements that will set values for the attached property. The type then iterates its child objects through internal logic against some object tree structure, obtains the values, and acts on those values in some manner.<strong>(Parent-Defined)</strong></p>
    
    <p>2.The type that defines the attached property will be used as the child element for a variety of possible parent elements and content models.<strong>(Child-Defined)</strong></p>
    
    <p>3.The type that defines the attached property represents a service. Other types set values for the attached property. Then, when the element that set the property is evaluated in the context of the service, the attached property values are obtained through internal logic of the service class.(<strong>Use as a Common Service</strong>)</p>
  </blockquote>
  
  <hr>
  
  <h2><strong>2. Trial</strong></h2>
  
  <p>Since Attached Property Can be defined by user,I thoought maybe we can use "CallBackMethod" to handle it.So I have coded some trials to validate my idea (Part 4.Code):</p>
  
  <blockquote>
    <p>1.Customize a child control ("Son") which defined a attached property named "CellBorderThicknessProperty" and use "PropertyChangedCallBack" to update the layout;</p>
    
    <p>2.Create a parent control ("Father") which's template contains the child control.</p>
    
    <p>3.Use the parent control in a Window and set the child.CellBorderThickness's value;</p>
  </blockquote>
  
  <hr>
  
  <h2><strong>3. Problem</strong></h2>
  
  <blockquote>
    <p>1.As you see,It's not a good way to expose the "Parent Type" in "Child Type",especially we won't how many Parents there would be...</p>
    
    <p>2.This Trial didnt work well,'Cus when the "PropertyChangedCallBack" was fired,the Father's template was not applied
    yet!So,Father.SetBorderThickness() will do nothing!</p>
  </blockquote>
  
  <h2>Is there any example which has used the Child-Defined attached property?And how does it work?</h2>
  
  <p>I am so eager to know how MS developers do with the Child-Defined ones.</p>
  
  <p>eg: what about <strong>ScrollViwer.VerticalScrollBarVisibility in WPFToolkit:DataGrid</strong>?</p>
  
  <hr>
  
  <h2><strong>4. Code</strong></h2>
  
  <h2>Son Control (Child)</h2>
  
  <pre><code>&lt;Style TargetType={x:Type Son}&gt;
     &lt;Setter Property="Template"&gt;
       &lt;Setter.Value&gt;
         &lt;ControlTemplate&gt;
           &lt;Border BorderThickness={TemplateBinding CellBorderThickness}&gt;
             ...
           &lt;/Border&gt;
         &lt;/ControlTemplate&gt;
       &lt;Setter.Value&gt;
     &lt;/Setter&gt;
  </code></pre>
  
  <h2>     </h2>
  
  <pre><code> public class Son: Control
   {
        public static readonly DependencyProperty CellBorderThicknessProperty = DependencyProperty.RegisterAttached("CellBorderThickness", typeof(Thickness), typeof(Son), new FrameworkPropertyMetadata(new Thickness(0.2), FrameworkPropertyMetadataOptions.AffectsRender, CellBorderThicknessProperty_ChangedCallBack));
  
        public static void SetCellBorderThickness(UIElement obj, Thickness value)
        {
          obj.SetValue(Son.CellBorderThicknessProperty, value);
        }
  
        public static Thickness GetCellBorderThickness(UIElement obj)
        {
          return (Thickness)obj.GetValue(Son.CellBorderThicknessProperty);
        }
  
        public Thickness CellBorderThickness
        {
          //With this, CellBorderThickness can be used as a normal dependency property.
          get { return (Thickness)GetValue(CellBorderThicknessProperty); }
          set { SetValue(CellBorderThicknessProperty, value); }
        }
  
        static void CellBorderThicknessProperty_ChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          if ((d as Father) != null)
          {
              // Try to update the Son's CellBorderThickness which is the child element of  the Father.
              d.SetBorderThickness(e.NewValue);
          }
        }
   }
  </code></pre>
  
  <h2>Father Control (Parent)</h2>
  
  <pre><code> &lt;Style TargetType={x:Type Father}&gt;
     &lt;Setter Property="Template"&gt;
       &lt;Setter.Value&gt;
         &lt;ControlTemplate&gt;
           &lt;Border&gt;
             &lt;Son&gt;&lt;/Son&gt;
           &lt;/Border&gt;
         &lt;/ControlTemplate&gt;
       &lt;Setter.Value&gt;
     &lt;/Setter&gt;
  </code></pre>
  
  <h2>     </h2>
  
  <pre><code> public class Father:Control
   {
     private Son childControl;
  
     public void override OnApplyTemplate()
     {
       childControl=(Son)GetTemplateChild("PART_ChildControl");//Here is a problem!!
     }
  
     public void SetBorderThickness(Thickness value)
     {
       if(childControl==null)
         childControl.CellBorderThickness=value;
     }
   }
  </code></pre>
  
  <h2>Window</h2>
  
  <pre><code> &lt;Window&gt;
     &lt;Grid&gt;
       &lt;Father Son.CellBorderThichness="5"&gt;&lt;/Father&gt;
     &lt;/Grid&gt;
   &lt;Window&gt;
  </code></pre>