Боюсь, что это невозможно без некоторого кода, но используя отражение и dynamic
, вы можете создать конвертер, который может это сделать (это было бы возможно без dynamic
, но более сложным):
public class IndexerConverter : IValueConverter
{
public string CollectionName { get; set; }
public string IndexName { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Type type = value.GetType();
dynamic collection = type.GetProperty(CollectionName).GetValue(value, null);
dynamic index = type.GetProperty(IndexName).GetValue(value, null);
return collection[index];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Положите в ресурсы следующее:
<local:IndexerConverter x:Key="indexerConverter" CollectionName="Items" IndexName="Index" />
и используйте его так:
<Label Content="{Binding Converter={StaticResource indexerConverter}}"/>
РЕДАКТИРОВАТЬ: Предыдущее решение не обновляется должным образом при изменении значений, это делает:
public class IndexerConverter : IMultiValueConverter
{
public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
{
return ((dynamic)value[0])[(dynamic)value[1]];
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
В ресурсах:
<local:IndexerConverter x:Key="indexerConverter"/>
Использование:
<Label>
<MultiBinding Converter="{StaticResource indexerConverter}">
<Binding Path="Items"/>
<Binding Path="Index"/>
</MultiBinding>
</Label>