views:

222

answers:

2

Today, I was working on a WPF UserControl to display the current value of a few variables. I was wondering if there would be a way to make a super simple property grid in WPF. The problem is on the starred line of the XAML below. How would I bind a string to a property with an ItemTemplate like I have setup below? To be more clear can I embed bindings inside of one another {Binding Path={Binding Value}}.

Here is the class:

public class Food
{
    public string Apple { get; set; }
    public string Orange { get; set; }
    public IEnumerable<KeyValuePair<string, string>> Fields
    {
        get
        {
            yield return new KeyValuePair<string, string>("Apple Label", "Apple");
            yield return new KeyValuePair<string, string>("Orange Label", "Orange");
        }
    }
}

And here is the XAML:

<UserControl x:Class="MAAD.Plugins.FRACTIL.Simulation.SimulationStateView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="331" Width="553">
 <ListView ItemSource="{Binding Fields}">
  <ListView.ItemTemplate>
   <DataTemplate>
    <StackPanel Orientation="Horizontal">
     <TextBlock Text="{Binding Key}" />
     **<TextBlock Text="{Binding Path={Binding Value}}" />**
    </StackPanel>
   </DataTemplate>
  </ListView.ItemTemplate>
 </ListView>
</UserControl>
+1  A: 

Well, it should just be:

<TextBlock Text="{Binding Path=Value}" />

However, I think I understand what you're trying to do. The problem is KeyValuePair isn't an INotifyPropertyChange subtype (rightfully so, won't get into that) so you'll never get a notification from it if the value is changed in the dictionary. Also, KeyValuePair is actually a struct. So, changing the value on the bound copy will not update the actual data source because it's a copy of the data.

If the Model you're working with actually is KeyValuePair, you would need to create a more specific View-Model class to enable this databinding scenario. This would need to be some kind of class that wraps the Key and has a reference to the underlying source (probably a Dictionary?) and actually makes the calls to update the value on the underlying source when its property is changed. That said, you still won't get notifications out of a Dictionary (again, assuming that's your source) because it doesn't fire any, so you won't be able to provide forward change notifications.

Drew Marsh
Thanks for the post, but I'm not looking to bind to Value but to the property with the name that Value holds. From the above example, one of the pairs would bind to the value of the Orange property and the other to the Apple property.
Jake Pearson
Ahhhhhhhhhhhh, I see what you're saying. Yeah, that's just not possible. Let me think about it and revise my answer.
Drew Marsh
+1  A: 

The essence of the problem is that you need not just the list of field descriptions and names, but the actual object that has those fields and names.

You can use a converter like that adds target references to the field objects and provides a value accessor, like this:

  public class PropertyValueAccessConverter : IMultiValueConverter
  {
    object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
      var target = values[0];
      var fieldList = values[1] as IEnumerable<KeyValuePair<string,string>>;
      return
        from pair in fieldList
        select new PropertyAccessor
        {
          Name = pair.Name,
          Target = target,
          Value = target.GetType().GetProperty(target.Value),
        };
    }

    object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
      throw new InvalidOperationException();
    }

    public class PropertyAccessor
    {
      public string Name
      public object Target;
      public PropertyInfo Property;

      public object Value
      {
        get { return Property.GetValue(Target, null); }
        set { Property.SetValue(Target, value, null); }
      }
    }

    public static PropertyValueAccessConverter Instance = new PropertyValueAccessConverter();
  }

With this converter you can bind your ItemsSource like this:

  <ListView>
    <ListView.ItemsSource>
      <MultiBinding Converter="{x:Static local:PropertyValueAccessConverter.Instance}">
        <Binding />
        <Binding Path="Fields" />
      </MultiBinding>
    </ListView.ItemsSource>
  </ListView>

By the way, a much more efficient way to implement your Fields property is:

  public IEnumerable<KeyValuePair<string, string>> Fields
  {
    get
    {
      return new KeyValuePair<string, string>[]
      {
        new KeyValuePair<string, string>("Apple Label", "Apple");
        new KeyValuePair<string, string>("Orange Label", "Orange");
      }
    }
  }

Though frankly I would use description attributes on the individual properties along with reflection instead of hard-coding the list. That would also eliminate the need for the MultiBinding in the first place.

Ray Burns