The essence of the problem is that you need not only a list of descriptions and field names, but also the actual object that has these fields and names.
, , :
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();
}
ItemsSource :
<ListView>
<ListView.ItemsSource>
<MultiBinding Converter="{x:Static local:PropertyValueAccessConverter.Instance}">
<Binding />
<Binding Path="Fields" />
</MultiBinding>
</ListView.ItemsSource>
</ListView>
, Fields:
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");
}
}
}
, . MultiBinding .