title: WPF DataTemplate performance
body: <p>Well the problem here is that if you have one DataTemplate for a type of object, each time the object is present, the DataTemplate will be created, that sounds about right.</p>
  
  <p>My problem is that our DataTemplate is sort of heavy AND the app could have around 1000 objects using that DataTemplate.</p>
  
  <p>I created a simplistic example where I created 5 Person objects and I bind them to a ListView. The DataTemplate for the Person object is a Grid with a Label and a ContextMenu with 2 MenuItems. For simplicity I just want to concentrate in the MenuItems. Using a memory profiler, I can see that there are a total of 10 MenuItem objects (2 per Person, 5 Person * 2 MenuItem = 10 MenuItem), and I want to know if there is a way to avoid this. Say, every Person object should share the same MenuItem reference, to avoid duplicating the DataTemplate each time a person is created.</p>
  
  <p>These are the results in the memory profiler.</p>
  
  <p><a href="https://i.stack.imgur.com/HCxnU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HCxnU.png" alt=""></a></p>
  
  <p>Thanks!</p>
  
  <p>This is my code:</p>
  
  <p>C#</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Windows;
  
  namespace MenuItemsTest
  {
      /// &lt;summary&gt;
      /// Interaction logic for Window1.xaml
      /// &lt;/summary&gt;
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
  
              this.DataContext = new List&lt;Person&gt;()
              {
                  new Person("Jim Morrison"),
                  new Person("Jimmy Page"),
                  new Person("Jimmy Hendrix"),
                  new Person("Janis Joplin"),
                  new Person("Peter Frampton")
              };
          }
      }
  
      public class Person
      {
          public string Name { get; set; }
  
          public Person(string name)
          {
              this.Name = name;
          }
      }
  }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="MenuItemsTest.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MenuItemsTest"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;DataTemplate DataType="{x:Type local:Person}"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ContextMenu&gt;
                      &lt;ContextMenu&gt;
                          &lt;MenuItem Header="Select" /&gt;
                          &lt;MenuItem Header="Deselect" /&gt;
                      &lt;/ContextMenu&gt;
                  &lt;/Grid.ContextMenu&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*" /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Label Content="{Binding Name}" /&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;ListView ItemsSource="{Binding}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>