title: How to draw on Canvas widgets embedded inside a ListView
body: <p>This is a really basic requirement, but I'm stuck!  For WPF/.Net - I just want to dynamically draw into a Canvas column in my ListView.  One failed attempt:</p>
  
  <pre><code>&lt;ListView name="myGridView"&gt;
      &lt;GridViewColumn Header="ColumnA" DisplayMemberBinding="{Binding Path=ColumnA}" /&gt;
      &lt;GridViewColumn DisplayMemberBinding="{Binding Path=ColumnB}"&gt;
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SUSPECT!
          &lt;Canvas Name="canvasColumn" Width="100" Height="20" /&gt;
      &lt;/GridViewColumn&gt;
  &lt;/GridView&gt;
  </code></pre>
  
  <p>Inside my code, I have a class "MyData" with fields bound to the non-canvas ListView columns.  I loop through some "Whatever"s creating items in the ListView:</p>
  
  <pre><code>foreach (Whatever whatever in whatevers)
  {
      MyData myData = new MyData();
      myData.ColumnA = whatever.A;
  
      myData.ColumnB = new Canvas();
      Line line = new Line();
      line.Stroke = System.Windows.Media.Brushes.Black;
      line.X1 = line.Y1 = 1;
      line.X2 = line.Y2 = 100;
      line.StrokeThickness = 1;
      myData.ColumnB.Children.Add(line);
  
      myListView.Items.Add(myData);
  }
  </code></pre>
  
  <p>This DOES NOT work: every row in the on-screen canvas column displays the text "System.Windows.Controls.Canvas".  Not terribly surprising - I've bound the column in the same way as the text columns and some toString conversion of the typename seems to kick in.  But, I've tried a few other things and just can't get the Canvas displayed.</p>
  
  <p>I have also tried removing the column binding marked "SUSPECT" above, and myData's ColumnB field, seeking a way to refer to the canvas widgets via the listview, i.e. something of the form:</p>
  
  <pre><code>myListView.reference-to-new-row-and-canvas-column = theNewCanvasIDrewOn;
  </code></pre>
  
  <p>Some of my searches have turned up ugly messes of Styles, ItemPanel configs etc.: please - if that's necessary, I at least hope it can be kept minimal....</p>
  
  <p>Any guidance greatly appreciated.</p>
  
  <p>Cheers,
  Tony</p>
  
  <h1>UPDATE</h1>
  
  <p>For my purposes, the minimal solution appears to be adding a DataTemplate to App.xaml's Application.Resources tag:</p>
  
  <pre><code>&lt;DataTemplate x:Key="myTemplate"&gt;
      &lt;Canvas Width="60" Height="20" Background="Red" ClipToBounds="True" &gt;
          &lt;ContentPresenter Content="{Binding myCanvasField}" /&gt;
      &lt;/Canvas&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>and defining a GridViewColumn as:</p>
  
  <pre><code>&lt;GridViewColumn CellTemplate="{StaticResource myTemplate}" Header="title" /&gt;
  </code></pre>
  
  <p>Thanks to Dean for pointing me in the right direction, and to <a href="https://stackoverflow.com/questions/3135438/binding-to-canvas">Binding to Canvas</a> for canvas-specific details.  I then "draw on" the Canvas property member of the object I add to the ListView.</p>