이 작업을 수행하는 유일한 방법은 MultiBinding 및 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>
그리고 당신의 계산기 :
public class SelectEmployeeConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
Debug.Assert(values.Length >= 2);
// change this type assumption
var array = values[0] as Array;
var list = values[0] as IList;
var enumerable = values[0] as IEnumerable;
var index = Convert.ToInt32(values[1]);
// and check bounds
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();
}
}
네 덕분에 단 하나의 클래스가있는 경우이 확실한 솔루션입니다. 하지만 이것과 비슷한 ViewModel 클래스가 많이 있습니다. 그래서 별도의 변환기가 필요하지 않습니다. 대신 Indexer 논리를 다른 것으로 변경합니다. –
나는 많은 컬렉션 유형에서 작동하도록이 글을 올리고 업데이트했다. – user7116