title: ListBox ItemTemplate - Display the property that the user selects at run time
body: <p>I have a <code>ListBox</code> which is bound to a <code>List</code> of <code>DataModel</code>. </p>
  
  <p>DataModel.cs</p>
  
  <pre><code>public class DataModel
  {
      public string Name { get; set; }
      public string Desc { get; set; }
      public string Code { get; set; }
  }
  </code></pre>
  
  <p>The <code>ListBox</code> should display two properties, so I have defined the <code>ItemTemplate</code> as below</p>
  
  <pre><code>&lt;ListBox.ItemTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="{Binding Name}"&gt;&lt;/TextBlock&gt;
              &lt;TextBlock Text=" - "&gt;&lt;/TextBlock&gt;
              &lt;TextBlock Text="{Binding Code}"&gt;&lt;/TextBlock&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ListBox.ItemTemplate&gt;
  </code></pre>
  
  <p>My requirement is that the user can select which two properties to display in the <code>ListBox</code> at run time. I am not sure how to achieve this. I have created a sample solution to explain my problem.</p>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
              &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;StackPanel Orientation="Horizontal" Margin="5"&gt;
              &lt;Label Content="Property One"&gt;&lt;/Label&gt;
              &lt;ComboBox Name="ComboBox1" Margin="3" Width="150" ItemsSource="{Binding DataModelProperties}"&gt;&lt;/ComboBox&gt;
              &lt;Label Content="Property Two"&gt;&lt;/Label&gt;
              &lt;ComboBox Name="ComboBox2" Margin="3" Width="150" ItemsSource="{Binding DataModelProperties}"&gt;&lt;/ComboBox&gt;
              &lt;Button Content="Go" Click="ButtonBase_OnClick" Margin="3"&gt;&lt;/Button&gt;
          &lt;/StackPanel&gt;
          &lt;ListBox Grid.Row="1" ItemsSource="{Binding DataModelList}" Margin="5"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;TextBlock Text="{Binding Name}"&gt;&lt;/TextBlock&gt;
                          &lt;TextBlock Text=" - "&gt;&lt;/TextBlock&gt;
                          &lt;TextBlock Text="{Binding Code}"&gt;&lt;/TextBlock&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows;
  
  namespace WpfApplication1
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              DataContext = new ViewModel();
  
          }
  
          private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
          {
  
          }
      }
  
      public class ViewModel
      {
          public List&lt;String&gt; DataModelProperties { get; set; }
  
          public List&lt;DataModel&gt; DataModelList { get; set; }
  
          public ViewModel()
          {
              DataModelList = new List&lt;DataModel&gt;();
  
              DataModelList.Add(new DataModel() { Name = "Name1", Code = "Code1", Desc = "Desc1" });
              DataModelList.Add(new DataModel() { Name = "Name2", Code = "Code2", Desc = "Desc2" });
              DataModelList.Add(new DataModel() { Name = "Name3", Code = "Code3", Desc = "Desc3" });
  
              DataModelProperties = typeof(DataModel).GetProperties().Select(s =&gt; s.Name).ToList();
          }
      }
  
      public class DataModel
      {
          public string Name { get; set; }
          public string Desc { get; set; }
          public string Code { get; set; }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Things I have tried</p>
  
  <pre><code>&lt;TextBlock Text="{Binding ElementName=ComboBox1, Path=SelectedItem}"&gt;&lt;/TextBlock&gt;
  </code></pre>
  
  <p>This just displays the selected property name.</p>