title: Create ItemTemplate for ListBox in code-beind in WPF
body: <p>I'm trying to create an ItemTemplate for a ListBox programmatically but it doesn't work. I know in XAML I can have something like:</p>
  
  <pre><code>&lt;ListBox x:Name="listbox" BorderThickness="0" Margin="6" Height="400"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Margin="0" Background="Red" Foreground="White" FontSize="18" Text="{Binding}" /&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>But when I'm trying to have the above result programmatically I face a problem which is binding the <code>TextBox.TextProperty</code>:</p>
  
  <pre><code>var textblock = new FrameworkElementFactory(typeof(TextBlock));
  
  // Setting some properties
  textblock.SetValue(TextBlock.TextProperty, ??);
  var template = new ControlTemplate(typeof(ListBoxItem));
  template.VisualTree = textblock;
  </code></pre>
  
  <p>Please help me on this issue. I couldn't find anything on the web about it.</p>
  
  <p>Thanks in advance.</p>