Mar*_*bic 14 .net data-binding wpf xaml indexer
<TextBlock Text="{Binding Path=[0]} />
Run Code Online (Sandbox Code Playgroud)
要么
<TextBlock Text="{Binding Path=[myKey]} />
Run Code Online (Sandbox Code Playgroud)
工作良好.但有没有办法将变量作为索引键传递?
<TextBlock Text="{Binding Path=[{Binding Column.Index}]} />
Run Code Online (Sandbox Code Playgroud)
Joh*_*wen 13
处理此问题的最快方法通常是使用MultiBinding和IMultiValueConverter接受集合及其绑定的索引:
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ListIndexToValueConverter}">
<Binding /> <!-- assuming the collection is the DataContext -->
<Binding Path="Column.Index"/>
</MultiBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)
然后转换器可以根据这两个值进行查找,如下所示:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
return Binding.DoNothing;
IList list = values[0] as IList;
if (list == null || values[1] == null || !(values[1] is int))
return Binding.DoNothing;
return list[(int)values[1]];
}
Run Code Online (Sandbox Code Playgroud)