title: create datatemplate code behind
body: <p>I am trying to create a ListBox view for show data, and I want it to contain a ListBox with a datatemplate for 2 columns "Product ID &amp; Product Barcode" </p>
  
  <p>I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.</p>
  
  <p>What I have made until now is:
  In XAML :</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="37*" /&gt;
          &lt;RowDefinition Height="88*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;TextBlock Text="Type Your Search :" HorizontalAlignment="Left"  VerticalAlignment="Bottom" Width="112" Height="15.96" Margin="20,0,0,4" /&gt;
  
      &lt;TextBox HorizontalAlignment="Right" VerticalAlignment="Bottom" Height="25" Width="300" Margin="0,0,44,0" x:Name="txtCAuto" TextWrapping="NoWrap" HorizontalContentAlignment="Right" /&gt;
  
      &lt;ListBox x:Name="lbSuggestion" SelectionChanged="lbSuggestion_SelectionChanged" Foreground="Black" Width="300" Margin="0,0,44,0"  FlowDirection="RightToLeft" Background="LightYellow" Grid.Row="1" Visibility="Collapsed"  ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemsSource="{Binding}"  HorizontalAlignment="Right" VerticalAlignment="Top" HorizontalContentAlignment="Right" BorderBrush="Transparent"  Grid.IsSharedSizeScope="True"&gt;
      &lt;/ListBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>In Code Behind:</p>
  
  <pre><code>string typedString = txtCAuto.Text.ToUpper();
          List&lt;string&gt; autoList = new List&lt;string&gt;();
          autoList.Clear();
  
           prodDetails ps = SelProd4Sale();
  
          foreach (string item in ps.ProdBrcdList)
          {
              if (!string.IsNullOrEmpty(txtCAuto.Text))
              {
                  if (item.StartsWith(typedString))
                  {
                      //autoList.Add(item);
                      FrameworkElementFactory colProdID = new FrameworkElementFactory(typeof(TextBlock));
                      Binding prodID = new Binding(ps.ProdIDList.ToString());
                      colProdID.SetBinding(TextBlock.TextProperty, prodID);
  
                      FrameworkElementFactory colProdBarcode = new FrameworkElementFactory(typeof(TextBlock));
                      Binding prodBarcode = new Binding();
                      prodBarcode.Path = new PropertyPath(ps.ProdBrcdList.ToString());
                      colProdBarcode.SetBinding(TextBlock.TextProperty, prodBarcode);
  
  
                      FrameworkElementFactory sb = new FrameworkElementFactory(typeof(StackPanel));
                      sb.AppendChild(colProdID);
                      sb.AppendChild(colProdBarcode);
  
                      dTemplate = new DataTemplate { VisualTree = sb };
                      dTemplate.Seal();
  
  
                  }
              }
          }
  
          if (autoList.Count &gt; 0)
          {
              lbSuggestion.ItemTemplate = dTemplate;
              //lbSuggestion.ItemsSource = autoList;
              lbSuggestion.Visibility = Visibility.Visible;
          }
          else if (txtCAuto.Text.Equals(""))
          {
              lbSuggestion.Visibility = Visibility.Collapsed;
              lbSuggestion.ItemsSource = null;
          }
          else
          {
              lbSuggestion.Visibility = Visibility.Collapsed;
              lbSuggestion.ItemsSource = null;
          }
  </code></pre>
  
  <p>but there is no data appears, any suggestion please.
  thanks,</p>