title: How to bind the DataTemplate within an ItemsControl
body: <p>I do not understand the DataTemplate within the ItemTemplate. I have an ObservableCollection"StringViewModel" Transcription that provides the ItemsSource for an ItemsControl. Populating the Transcription collection with StringViewModel correctly displays these strings.</p>
  
  <p>At the time of displaying each string, I would like the XAML binding to call MyConverter such that additional code can be run on each item being displayed. (I am not trying to change what is displayed, but only perform some actions based on the position of what is displayed).</p>
  
  <p>In the following code, MyConverter is never called. </p>
  
  <p>What is the best way to call MyConverter on each item presented in the ItemsControl?</p>
  
  <p>Any help is appreciated.</p>
  
  <p>C#</p>
  
  <pre><code>public class StringViewModel : FrameworkElement {...}
  
  private ObservableCollection&lt;StringViewModel&gt; transcription = new ObservableCollection&lt;StringViewModel&gt;();
      public ObservableCollection&lt;StringViewModel&gt; Transcription
      {
          get
          {
              return transcription;
          }
          set
          {
              transcription = value;
          }
      }
  </code></pre>
  
  <p>XAML</p>
  
  <pre><code>&lt;ItemsControl 
                  ItemsSource="{Binding Transcription}"
                  &gt;
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;Canvas 
                              Background="Transparent"
                               Width="{x:Static h:Constants.widthCanvas}" 
                               Height="{x:Static h:Constants.heightCanvas}" 
                               /&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt; 
                  &lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate&gt;              &lt;!-- THIS DOES NOT WORK --&gt;
  
                          &lt;ContentControl Content="{Binding Converter={StaticResource MyConverter}}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
              &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>My attempt to change this to:</p>
  
  <pre><code>&lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StringViewModel ft="{Binding Path=., Converter={StaticResource MyConverter}}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
  </code></pre>
  
  <p>Results in:</p>
  
  <p>ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='StringViewModel'</p>
  
  <p>What to do?</p>
  
  <p>The StringViewModel is defined as:</p>
  
  <pre><code>  public class StringViewModel : FrameworkElement, INotifyPropertyChanged
  {
  
      public StringViewModel()
      {
      }
  
      public StringViewModel(
          Point topleft, 
          string text, 
          double fontsizediu,                         
          SolidColorBrush brush, 
          Func&lt;FormattedText,FormattedText&gt; f,        
          double lineheight)
      {
          this.text = text;
          this.emSize = fontsizediu;                 
          this.color = brush;
          this.topleft = topleft;
          this.lineheight = lineheight;              
          this.f = f;
      }
  
      protected override void OnRender(DrawingContext dc)
      {
  
          ft = new FormattedText(
              text, 
              CultureInfo.CurrentCulture, 
              FlowDirection.LeftToRight,
              new Typeface(new FontFamily("Segoe Script"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal),
              emSize, 
              color);
  
  
          ft.TextAlignment = TextAlignment.Left;
  
          // apply special styles
          ft = f(ft);
          dc.DrawText(ft, topleft);
  
      }
  
      private string text;
      private double emSize;
      private SolidColorBrush color;
      private Func&lt;FormattedText, FormattedText&gt; f;
  
      public Point topleft;
      private double? Lineheight;
  
  
      private FormattedText _ft;
      public FormattedText ft
      {
          get
          {
              return _ft;
          }
          set
          {
              if (_ft != value)
              {
                  _ft = value;
                  OnPropertyChanged("ft");
              }
          }
      }
  </code></pre>