The only way I found for this is MultiBinding and IMultiValueConverter .
<TextBlock DataContext="{Binding Source={x:Static vm:MainViewModel.Employees}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource conv:SelectEmployee}">
<Binding />
<Binding Path="SelectedEmployee" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
And your converter:
public class SelectEmployeeConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
Debug.Assert(values.Length >= 2);
var array = values[0] as Array;
var list = values[0] as IList;
var enumerable = values[0] as IEnumerable;
var index = Convert.ToInt32(values[1]);
if (array != null && index >= 0 && index < array.GetLength(0))
return array.GetValue(index);
else if (list != null && index >= 0 && index < list.Count)
return list[index];
else if (enumerable != null && index >= 0)
{
int ii = 0;
foreach (var item in enumerable)
{
if (ii++ == index) return item;
}
}
return Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
source
share