title: listbox isSelected databinding in DataTemplate
body: <p>I try to simply databind <code>IsSelected</code> property with <code>IsSelected</code> field in my class. But after I change the value in code, it doesn't change the property, neither does clicking on <code>ListBoxItem</code> change the field value.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;FlipView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" ... &gt;
      &lt;FlipView.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;UserControl Loaded="StartLayoutUpdates" 
                  Unloaded="StopLayoutUpdates"&gt;
                  &lt;!-- other controls --&gt;
                  &lt;ListBox Grid.Row="1" Grid.ColumnSpan="3"
                      SelectionMode="Multiple" VerticalAlignment="Center" 
                      ItemsSource="{Binding Answers}"&gt;
                      &lt;ListBox.Resources&gt;
                          &lt;local:LogicToText x:Key="logToText" /&gt;
                      &lt;/ListBox.Resources&gt;
  
                       &lt;!-- bind IsSelected only in one way from 
                           code to content --&gt; 
                       &lt;ItemsControl.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ListBoxItem 
                                IsSelected="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}" 
                                Content="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}"&gt;
  
                              &lt;/ListBoxItem&gt;
  
                          &lt;/DataTemplate&gt;
                      &lt;/ItemsControl.ItemTemplate&gt;
  
  
                      &lt;!-- not working at all
                      &lt;ListBox.Resources&gt;
                          &lt;Style TargetType="ListBoxItem"&gt;
                              &lt;Setter Property="IsSelected" 
                                  Value="{Binding IsSelected, Mode=TwoWay}"/&gt;
                              &lt;Setter Property="Content" 
                                  Value="{Binding IsSelected, Mode=TwoWay}"/&gt;
                          &lt;/Style&gt;
                      &lt;/ListBox.Resources&gt;--&gt;
  
                  &lt;/ListBox&gt;
              &lt;/UserControl&gt;
          &lt;/DataTemplate&gt;
      &lt;/FlipView.ItemTemplate&gt;
  &lt;/FlipView&gt;
  </code></pre>
  
  <p>Code: </p>
  
  <p>Answers</p>
  
  <pre><code>private ObservableCollection&lt;PrawoJazdyDataAnswer&gt; _answers = 
      new ObservableCollection&lt;PrawoJazdyDataAnswer&gt;();
  public ObservableCollection&lt;PrawoJazdyDataAnswer&gt; Answers 
  { 
      get 
      { 
         return this._answers; 
      }  
  }    
  </code></pre>
  
  <p>Single item(Answer)</p>
  
  <pre><code>public class PrawoJazdyDataAnswer : NPCHelper// PrawoJazdy.Common.BindableBase
  {
      public PrawoJazdyDataAnswer(String ans, bool ansb)
      {
          this._ans = ans;
          this._isSelected = ansb;
      }
  
      public override string ToString() 
      { 
          return _isSelected.ToString();  //Only For debug purposes 
                                          //normally return _ans 
      }
      private string _ans;
      public string Ans
      {
          get { return this._ans; }
          //set { this.SetProperty(ref this._ans, value); }
      }
  
      private bool _isSelected;
      public bool IsSelected
      {
          get { return this._isSelected; }
          set
          {
              _isSelected = value;
              FirePropertyChanged("IsSelected");
              //this.SetProperty(ref this._isSelected, value); 
          }
      }
  }
  </code></pre>
  
  <p>FirePropertyChanged</p>
  
  <pre><code>public class NPCHelper : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      public void FirePropertyChanged(string prop)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(prop));
      }
  }
  </code></pre>
  
  <p>Converter(which sometimes seems to be needed and others not..., I tried ~10 approaches from different tutorials/examples) </p>
  
  <pre><code>public class LogicToText : IValueConverter
  {
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      public object Convert(object value, Type targetType, 
                            object parameter, string language)
      {
          //if (value == null || (bool)value == false)
            //  return "False";
  
          return value.ToString();
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      public object ConvertBack(object value, Type targetType, 
                                object parameter, string language)
      {
          return value.ToString().Contains("True") ? true : false;
      }
  </code></pre>
  
  <p>Thanks in advance, and sorry for my English(still learning).</p>
  
  <p>@edit
  Thanks for quick reply.</p>
  
  <p>For test purposes I created a button and text block:</p>
  
  <pre><code>&lt;Button Click="spr" &gt;Sprawdź&lt;/Button&gt;
  &lt;TextBlock Text="{Binding Answers[0].IsSelected, Mode=TwoWay}" &gt; &lt;/TextBlock&gt; 
  </code></pre>
  
  <p>It's in other controls part (above list box, but in <code>FlipView</code>)
  Click method</p>
  
  <pre><code>private void spr(object sender, RoutedEventArgs e)
      {
          var ans = ((PrawoJazdyDataQuestion)this.flipView.SelectedItem).Answers;
          foreach (var item in ans)
              item.IsSelected = item.IsSelected ? false : true;
      }
  </code></pre>
  
  <p>As I wrote, when i'm changing data from code side, it is changing content of element, but not appearance of <code>ListBoxItem</code>. And if I just select it on <code>ListBox</code>, it doesn't change the data in <code>TextBlock</code> neither in <code>ListBox</code> itself.</p>
  
  <p>@edit2 fixing typos...</p>