title: WPF dynamic binding to property
body: <p>I have an ItemsControl that should display the values of some properties of an object.</p>
  
  <p>The ItemsSource of the ItemsControl is an object with two properties: <strong>Instance</strong> and <strong>PropertyName</strong>.</p>
  
  <p>What I am trying to do is displaying all the property values of the <em>Instance</em> object, but I do not find a way to set the Path of the binding to the <em>PropertyName</em> value:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource={Binding Path=InstanceProperties}&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding Path=PropertyName, Mode=OneWay}"/&gt;
                  &lt;TextBlock Text=": "/&gt;
                  &lt;TextBlock Text="{Binding Source=??{Binding Path=Instance}??, Path=??PropertyName??, Mode=OneWay}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>the question marks are the points where I don't know how to create the binding.</p>
  
  <p>I initially tried with a MultiValueConverter:</p>
  
  <pre><code>&lt;TextBlock Grid.Column="1" Text="{Binding}"&gt;
      &lt;TextBlock.DataContext&gt;
          &lt;MultiBinding Converter="{StaticResource getPropertyValue}"&gt;
              &lt;Binding Path="Instance" Mode="OneWay"/&gt;
              &lt;Binding Path="PropertyName" Mode="OneWay"/&gt;
          &lt;/MultiBinding&gt;
      &lt;/TextBlock.DataContext&gt;
  &lt;/TextBlock&gt;
  </code></pre>
  
  <p>The MultiValueConverter uses Reflection to look through the <em>Instance</em> and returns the value of the property.</p>
  
  <p>But if the property value changes, this change is not notified and the displayed value remains unchanged.</p>
  
  <p>I am looking for a way to do it with XAML only, if possible, if not I will have to write a wrapper class to for the items of the ItemsSource collection, and I know how to do it, but, since it will be a recurring task in my project, it will be quite expensive.</p>
  
  <h2>Edit:</h2>
  
  <p>For those who asked, <strong>InstanceProperties</strong> is a property on the ViewModel which exposes a collection of objects like this:</p>
  
  <pre><code>public class InstanceProperty : INotifyPropertyChanged
  {
      //[.... INotifyPropertyChanged implementation ....]
  
      public INotifyPropertyChanged Instance { get; set; }
  
      public string PropertyName { get; set; }
  
  }
  </code></pre>
  
  <p>Obviously the two properties notify theirs value is changing through INotifyPropertyChanged, I don't include the OnPropertyChanged event handling for simplicity.</p>
  
  <p>The collection is populated with a limited set of properties which I must present to the user, and I can't use a PropertyGrid because I need to filter the properties that I have to show, and these properties must be presented in a graphically richer way.</p>
  
  <p>Thanks</p>