title: Creating DataTemplate from code behind
body: <p>Sorry for the not so very general question...</p>
  
  <p>I have a ListView that I have to fill from code behind, and this ListView also need to get its GridViewColumn's from code behind.</p>
  
  <p>For strings it wasn't hard to make the connection, but now I wan't to create a Ellipse that represents a Boolean value in the ListView.</p>
  
  <p>The code in XAML is rather easy, but I fail at converting it to c# code.</p>
  
  <p>Here is parts of the XMAL code:</p>
  
  <pre><code>&lt;ResourceDictionary&gt;
      &lt;BooleanToVisibilityConverter x:Key="BoolToVisibility" /&gt;
      &lt;DataTemplate x:Key="templateAdmin"&gt;
          &lt;DockPanel&gt;
              &lt;Ellipse Width="8" Height="8" Visibility="{Binding Path=isAdmin, Converter={StaticResource BoolToVisibility}}" Fill="Black"/&gt;
          &lt;/DockPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  
  &lt;ListView&gt;
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last Name"/&gt;
              &lt;GridViewColumn CellTemplate="{StaticResource templateAdmin}"
                  &lt;GridViewColumnHeader"&gt;
                      &lt;TextBlock Text="S"/&gt;
                  &lt;/GridViewColumnHeader&gt;
              &lt;/GridViewColumn&gt;
          &lt;/GridView&gt;
      &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>And by now I have gotten this far:
  XAML:</p>
  
  <pre><code>&lt;local:SortableListView &gt;
      &lt;ListView.View&gt;
          &lt;GridView x:Name="GroupListGridView" /&gt;
      &lt;/ListView.View&gt;
  &lt;/local:SortableListView&gt;
  </code></pre>
  
  <p>And in code I have a <code>Collection&lt;GridViewColumn&gt; GridViewColumns</code> that I loop throug and add all items to the <code>GroupListGridView</code>. And I have a function to fill the <code>GridViewColumns</code> collection:</p>
  
  <pre><code>private void CreateGridViews()
  {
      //Creating the Text was easy!
      GridViewColumns.add(new GridViewColumn(){ Header = "LastName", DisplayMemberBinding = new Binding("LastName") });
  
      //Creating the Ellipse was harder!
      GridViewColumn gvc = new GridViewColumn();
      DataTemplate dt = new DataTemplate();
  
      gvc.DisplayMemberBinding = new Binding("isAdmin");
      FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Ellipse));
      fef.SetValue(Ellipse.WidthProperty, 8.0D);
      fef.SetValue(Ellipse.HeightProperty, 8.0D);
      fef.SetValue(Ellipse.FillProperty, new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black));
  
      //I'm guessing that somewhere here there should be some binding to the visibility property and some sort of conversion done... But I can't figure out how!
  
      dt.VisualTree = fef;
      gvc.CellTemplate = dt;
      GridViewColumns.Add(gvc);
  }
  </code></pre>
  
  <p>I don't think that I'm that far of... Just that I can't figure out those last steps!</p>