title: Referencing objects in XAML design data
body: <p>How can I reference the same object twice (or more often) in a XAML design data file?
  I tried to use <code>{x:Reference}</code>, but this does not seem to work.</p>
  
  <p>Here is an example:</p>
  
  <p>The combo box in the cells of the second column of the sample's data grid displays a list of "data types". The list of available data types comes from the <code>Types</code> property of the main window's view model (= the data context). The list of items in the grid comes from the <code>Items</code> property of the view model. Each item has a <code>Name</code> and a <code>Type</code> column, where Type references a data type object.</p>
  
  <p>The sample grid looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/nC8DV.jpg" alt="Screenshot"></p>
  
  <p>Here is the XAML design data which should show the same grid contents in the Visual Studio designer (but it doesn't):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;local:MainWindowViewModel
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataGridSample"
      &gt;
      &lt;local:MainWindowViewModel.Types&gt;
          &lt;local:DataType Name="String" x:Name="String"/&gt;
          &lt;local:DataType Name="Integer" x:Name="Integer"/&gt;
      &lt;/local:MainWindowViewModel.Types&gt;
      &lt;local:MainWindowViewModel.Items&gt;
          &lt;local:Item Name="Lorem" Type="{x:Reference String}"/&gt;
          &lt;local:Item Name="Ipsum" Type="{x:Reference Integer}"/&gt;
      &lt;/local:MainWindowViewModel.Items&gt;
  &lt;/local:MainWindowViewModel&gt;
  </code></pre>
  
  <p>Above, I am using <code>{x:Reference String}</code> to obtain a reference to the object that was created by <code>&lt;local:DataType Name="String" x:Name="String"/&gt;</code>.</p>
  
  <p>In the Visual Studio designer, the list is empty, and the error message "Errors found in markup: ... DesignData.xaml" is displayed. In the editor for the design data XAML files, I get the error message "Service provider is missing the INameResolver service".</p>
  
  <p>Is there any alternative to <code>{x:Reference}</code> which I could use in design data files to refer to an object?</p>
  
  <p>For completeness, here are the remaining files of my sample:</p>
  
  <p>MainWindow.xaml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="DataGridSample.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          mc:Ignorable="d"
          Title="Sample" Height="300" Width="400"
          d:DataContext="{d:DesignData Source=DesignData.xaml}"&gt;
      &lt;Window.Resources&gt;
          &lt;CollectionViewSource x:Key="types" Source="{Binding Types}"/&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*"/&gt;
                  &lt;DataGridComboBoxColumn SelectedItemBinding="{Binding Type}"
                                          ItemsSource="{Binding Source={StaticResource types}}" 
                                          DisplayMemberPath="Name"
                                          Header="Type" Width="*"/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace DataGridSample
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
  
          public MainWindow()
          {
              InitializeComponent();
  
              DataContext = _viewModel;
          }
      }
  }
  </code></pre>
  
  <p>MainWindowViewModel.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Text;
  
  namespace DataGridSample
  {
      public class MainWindowViewModel
      {
          private readonly ObservableCollection&lt;DataType&gt; _dataTypes;
          private readonly ObservableCollection&lt;Item&gt; _items;
  
          public MainWindowViewModel()
          {
              DataType typeString = new DataType {Name = "String"};
              DataType typeInteger = new DataType {Name = "Integer"};
  
              _dataTypes = new ObservableCollection&lt;DataType&gt; {typeString, typeInteger};
              _items = new ObservableCollection&lt;Item&gt;
                  {
                      new Item {Name = "Lorem", Type = typeString},
                      new Item {Name = "Ipsum", Type = typeInteger}
                  };
          }
  
          public ObservableCollection&lt;DataType&gt; Types
          {
              get
              {
                  return _dataTypes;
              }
          }
  
          public ObservableCollection&lt;Item&gt; Items
          {
              get
              {
                  return _items;
              }
          }
      }
  
      public class DataType
      {
          public string Name { get; set; }
      }
  
      public class Item
      {
          public string Name { get; set; }
  
          public DataType Type { get; set; }
      }
  }
  </code></pre>