title: How to define a dependency property in a user control that just bubbles up a value from one of its children?
body: <p>I am creating a ToggleSwitchItem user control, which contains a ToggleSwitch and a TextBlock. I have defined a dependency property called IsChecked which I just want to use to expose the IsChecked property of the private ToggleSwitch child.</p>
  
  <p>But the data binding doesn't work... It just stays at the default value when loaded. </p>
  
  <p>What am I missing?</p>
  
  <p>Code:</p>
  
  <pre><code>public static readonly DependencyProperty IsCheckedProperty =
      DependencyProperty.Register(
          "IsChecked",
          typeof(bool),
          typeof(ToggleSwitchItem),
          new PropertyMetadata(new PropertyChangedCallback
            (OnIsCheckedChanged)));
  
  public bool IsChecked
  {
    get
    {
      return (bool)GetValue(IsCheckedProperty);
    }
    set
    {
      SetValue(IsCheckedProperty, value);
    }
  }
  
  private static void OnIsCheckedChanged(DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    ToggleSwitchItem item = (ToggleSwitchItem)d;
    bool newValue = (bool)e.NewValue;
    item.m_switch.IsChecked = newValue;
  }
  </code></pre>
  
  <p>for the data binding, I'm using to following:</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage.Resources&gt;
      &lt;myApp:SharedPreferences x:Key="appSettings"/&gt;
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  
  IsChecked="{Binding Source={StaticResource appSettings}, 
      Path=SomeProperty, Mode=TwoWay}" 
  </code></pre>
  
  <p>The SharedPreferences class is working fine, as it works without issue when bound to a plain vanilla ToggleSwitch's IsChecked property exactly as per above.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>SOLUTION</strong> (with help from Anthony):</p>
  
  <p>I bind my child toggle switch to my user control in the user control's constructor like so:</p>
  
  <pre><code>Binding binding = new Binding();
  binding.Source = this;
  binding.Path = new PropertyPath("IsChecked");
  binding.Mode = BindingMode.TwoWay;
  m_switch.SetBinding(ToggleSwitch.IsCheckedProperty, binding);
  </code></pre>
  
  <p>And I remove the callback as I no longer need it:</p>
  
  <pre><code>public static readonly DependencyProperty IsCheckedProperty =
      DependencyProperty.Register(
          "IsChecked",
          typeof(bool),
          typeof(ToggleSwitchItem),
          null);
  
  public bool IsChecked
  {
    get
    {
      return (bool)GetValue(IsCheckedProperty);
    }
    set
    {
      SetValue(IsCheckedProperty, value);
    }
  }
  </code></pre>