使用股票 WPF,您需要使用IMultiValueConverter
:
public class IndexedValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
if (values.Length < 2) return null;
var index = Convert.ToInt32(values[1], culture);
var array = values[0] as Array;
if (array != null) return array.GetValue(index);
var list = values[0] as IList;
if (list != null) return list[index];
var enumerable = values[0] as IEnumerable;
if (enumerable != null)
{
int ii = 0;
foreach (var item in enumerable)
{
if (ii++ == index) return item;
}
}
return null;
}
// ... Implement ConvertBack as desired
然后在您的 XAML 中:
<Label>
<Label.Resources>
<local:IndexedValueConverter x:Key="Indexer" />
</Label.Resources>
<Label.Content>
<MultiBinding Converter="{StaticResource Indexer}">
<Binding Path="MyStringCollection"
Source="{x:Static p:Settings.Default}" />
<Binding Path="SelectedIndex"
Source="{x:Static p:Settings.Default}" />
</MultiBinding>
</Label.Content>
</Label>