title: c# Override a dependency property of an system control
body: <p>Hi I have read through some of the relevant questions in stackoverflow but still can't figure out how to solve my questions:</p>
  
  <p>I need to creat a CheckableGroupBox control which is same as GroupBox but has a checkbox in the header. And it has the following requirement:</p>
  
  <ol>
  <li><p>The original GroupBox has a Header property that is of Object type, I need to limit this property in the CheckableGroupBox to be string only. </p></li>
  <li><p>Users can change the header's text (the checkbox's text) by calling myCheckableGroupBox.Header="some text here".</p></li>
  </ol>
  
  <p>I wrote the below code to serve these proposes:</p>
  
  <pre><code>public class CheckableGroupBox : System.Windows.Controls.GroupBox
  {
      //override the default header property
      private static void OnHeaderChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          CheckableGroupBox me = o as CheckableGroupBox;
          me.labHeader.Content = e.NewValue.ToString();        
      }
  
      public CheckableGroupBox() : base()
      {
          //setup the header of the group box
  
          //create the stack panel, add the checkbox and the label
          System.Windows.Controls.StackPanel sp = new System.Windows.Controls.StackPanel();
          sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
          this.chkHeader = new System.Windows.Controls.CheckBox();
          sp.Children.Add(this.chkHeader);
          sp.Children.Add(this.labHeader);
  
          //set the header to be the stack panel.
          this.Header = sp;
  
          //override the default HeaderProperty
          CheckableGroupBox.HeaderProperty.OverrideMetadata(
              typeof(object), 
              new PropertyMetadata("", OnHeaderChangedCallback)
              );
      }
  </code></pre>
  
  <p>}</p>
  
  <p>However, the code doesn't work. When I add a CheckableGroupBox(in the design view) to a window, it prompts:</p>
  
  <p>Cannot create an instance of "CheckableGroupBox", Object' type must derive from DependencyObject.</p>
  
  <p>Any ideas?</p>