title: Binding ListBox Items using Templates
body: <p>I am going crazy trying to figure this out without success.</p>
  
  <p>I have a DependencyObject, ("UserObject").  It has a "DataItems" DependecyProperty that is an ObservableCollection. "UserDefiniton" is a DependencyObject with a DependencyProperty of "Data".  Data has two properties: DataType (an enumeration) and Value (a string).</p>
  
  <p>I am trying to define a ListBox in XAML that uses the "DataItems" property as its ItemsSource.  In the ItemTemplate, I have several different controls.  For simplicity of this issue, I am using a CheckBox and a TextBox.  I want CheckBox to be available and visible when DataType is 0, while I want the TextBox to be available and visible when the DataType is 1.  Only one control can be available and visible at a time.</p>
  
  <p>This works:</p>
  
  <pre><code>&lt;ListBox
      ItemsSource={Binding DataItems, Mode=OneWay}&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel&gt;
                  &lt;CheckBox
                      Visibility="{Binding Path=Data.DataType, Mode=OneWay, Converter={StaticResource VisibilityConverter}, ConverterParameter=0}"
                      IsChecked="{Binding Path=Data.Value, Mode=TwoWay, Converter={StaticResource StringToBoolean}}" /&gt;
                  &lt;TextBox
                      Visibility="{Binding Path=Data.DataType, Mode=OneWay, Converter={StaticResource VisibilityConverter}, ConverterParameter=1}"
                      Text="{Binding Path=Data.Value, Mode=TwoWay}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;Listbox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>The problem is that even though only one is visible, both are fighting over the Data.Value property (the boolean of the checkbox will show in the textbox, even though the checkbox is hidden).</p>
  
  <p>Basically, though, the binding in this case is working--but the implementation is incorrect.</p>
  
  <p>So, I switched to using Templates.  The problem I am having is that I can't get the binding to work.</p>
  
  <p>This is the code that I have for the Template.  The Template selector is working correctly, but the Text property of the TextBox and the IsChecked property of the checkbox are not binding to Data.Value:</p>
  
  <pre><code>  &lt;DataTemplate x:Key="TextBoxItem"&gt;
                  &lt;TextBox
                      Text="{Binding Path=Data.Value, Mode=TwoWay}" /&gt;
    &lt;/DataTemplate&gt;
    &lt;DataTemplate x:Key="CheckBoxItem"&gt;
                  &lt;CheckBox
                      IsChecked="{Binding Path=Data.Value, Mode=TwoWay, Converter={StaticResource StringToBoolean}}" /&gt;
    &lt;/DataTemplate&gt;
  
  
  ...
  
  &lt;ListBox
      ItemsSource={Binding DataItems, Mode=OneWay}&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;ContentControl
                  Content="{Binding Path=Data.DataType, Mode=OneWay}"
                           ContentTemplateSelector="{DynamicResource UserDefinitionTemplateSelector}"/&gt;
  
          &lt;/DataTemplate&gt;
      &lt;ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>So how do I fix the binding?</p>