How can I refer to the same object twice (or more often) in the XAML project data file? I tried to use {x:Reference} , but this does not work.
Here is an example:
The combo box in the cells of the second column of the sample data grid displays a list of "data types". The list of available data types comes from the Types property of the main window model (= data context). The list of elements in the grid comes from the Items property of the view model. Each element has columns Name and Type , where Type refers to an object of a data type.
The sample grid is as follows:

Here is the XAML design data that should show the same grid content in the Visual Studio designer (but itβs not):
<?xml version="1.0" encoding="utf-8" ?> <local:MainWindowViewModel xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DataGridSample" > <local:MainWindowViewModel.Types> <local:DataType Name="String" x:Name="String"/> <local:DataType Name="Integer" x:Name="Integer"/> </local:MainWindowViewModel.Types> <local:MainWindowViewModel.Items> <local:Item Name="Lorem" Type="{x:Reference String}"/> <local:Item Name="Ipsum" Type="{x:Reference Integer}"/> </local:MainWindowViewModel.Items> </local:MainWindowViewModel>
Above, I use {x:Reference String} to get a reference to an object created using <local:DataType Name="String" x:Name="String"/> .
In the Visual Studio designer, the list is empty and the error message "Errors detected in the markup: ... DesignData.xaml" appears. In the editor for the project data XAML files, I get the error "The service provider does not have the INameResolver service."
Is there an alternative {x:Reference} that I could use in project data files to refer to an object?
For completeness, here are the rest of my sample files:
MainWindow.xaml:
<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}"> <Window.Resources> <CollectionViewSource x:Key="types" Source="{Binding Types}"/> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*"/> <DataGridComboBoxColumn SelectedItemBinding="{Binding Type}" ItemsSource="{Binding Source={StaticResource types}}" DisplayMemberPath="Name" Header="Type" Width="*"/> </DataGrid.Columns> </DataGrid> </Grid> </Window>
MainWindow.xaml.cs:
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 {
MainWindowViewModel.cs:
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; namespace DataGridSample { public class MainWindowViewModel { private readonly ObservableCollection<DataType> _dataTypes; private readonly ObservableCollection<Item> _items; public MainWindowViewModel() { DataType typeString = new DataType {Name = "String"}; DataType typeInteger = new DataType {Name = "Integer"}; _dataTypes = new ObservableCollection<DataType> {typeString, typeInteger}; _items = new ObservableCollection<Item> { new Item {Name = "Lorem", Type = typeString}, new Item {Name = "Ipsum", Type = typeInteger} }; } public ObservableCollection<DataType> Types { get { return _dataTypes; } } public ObservableCollection<Item> 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; } } }