title: WPF: Binding to ObservableCollection in ControlTemplate is not updated
body: <p>I created a <code>ControlTemplate</code> for my custom control <code>MyControl</code>.</p>
  
  <p><code>MyControl</code> derives from <code>System.Windows.Controls.Control</code> and defines the following property <code>public ObservableCollection&lt;MyControl&gt; Children{ get; protected set; }</code>.</p>
  
  <p>To display the nested child controls I am using an <code>ItemsControl</code> (<code>StackPanel</code>) which is surrounded by a <code>GroupBox</code>. If there are no child controls, I want to hide the <code>GroupBox</code>.</p>
  
  <p>Everything works fine on application startup: The group box and child controls are shown if the Children property initially contained at least one element. In the other case it is hidden.</p>
  
  <p>The problem starts when the user adds a child control to an empty collection. The <code>GroupBox</code>'s visibility is still collapsed. The same problem occurs when the last child control is removed from the collection. The <code>GroupBox</code> is still visible.
  Another symptom is that the <code>HideEmptyEnumerationConverter</code> converter does not get called.
  Adding/removing child controls to non empty collections works as expected.</p>
  
  <p>Whats wrong with the following binding? Obviously it works once but does not get updated, although the collection I am binding to is of type <code>ObservableCollection</code>.</p>
  
  <pre><code>&lt;!-- Converter for hiding empty enumerations --&gt;
  &lt;Common:HideEmptyEnumerationConverter x:Key="hideEmptyEnumerationConverter"/&gt;
  &lt;!--- ... ---&gt;
  
  &lt;ControlTemplate TargetType="{x:Type MyControl}"&gt;
    &lt;!-- ... other stuff that works ... --&gt;
    &lt;!-- Child components --&gt;
    &lt;GroupBox Header="Children"
              Visibility="{Binding RelativeSource={RelativeSource TemplatedParent},
                Path=Children, Converter={StaticResource hideEmptyEnumerationConverter}}"&gt;
      &lt;ItemsControl ItemsSource="{TemplateBinding Children}"/&gt;
    &lt;/GroupBox&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>[ValueConversion(typeof (IEnumerable), typeof (Visibility))]
  public class HideEmptyEnumerationConverter : IValueConverter
  {
      #region IValueConverter Members
  
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          int itemCount = ((IEnumerable) value).Cast&lt;object&gt;().Count();
          return itemCount == 0 ? Visibility.Collapsed : Visibility.Visible;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  
      #endregion
  }
  </code></pre>
  
  <p>Another, more general question: How do you guys debug bindings? Found this (<a href="http://bea.stollnitz.com/blog/?p=52" rel="nofollow noreferrer">http://bea.stollnitz.com/blog/?p=52</a>) but still I find it very hard to do.</p>
  
  <p>I am glad for any help or suggestion.</p>