2010-12-22 13 views
12
<TextBlock Text="{Binding Path=[0]} /> 

oWPF se unen a indexador

<TextBlock Text="{Binding Path=[myKey]} /> 

funciona bien. Pero, ¿hay alguna manera de pasar una variable como clave del indexador?

<TextBlock Text="{Binding Path=[{Binding Column.Index}]} /> 
+0

enlace de MSDN para futuros buscadores: http://msdn.microsoft.com/en-us/library/ms742451.aspx – yzorg

+0

Compruebe mi respuesta en este enlace http://stackoverflow.com/q/ 4385693/217880 – biju

Respuesta

0

Esto exige un IValueConverter IMultiValueConverter.

+1

¿De verdad? ¿Puedes describir cómo harías esto con un convertidor de valor? – ColinE

0

No, me temo que no. En este punto, probablemente debas considerar la posibilidad de crear un modelo de vista que dé forma a tus datos para facilitar su vinculación.

12

La forma más rápida de manejar esto es por lo general a utilizar un MultiBinding con un IMultiValueConverter que acepta la recopilación y el índice de sus consolidaciones:

<TextBlock.Text> 
    <MultiBinding Converter="{StaticResource ListIndexToValueConverter}"> 
     <Binding /> <!-- assuming the collection is the DataContext --> 
     <Binding Path="Column.Index"/> 
    </MultiBinding> 
</TextBlock.Text> 

el convertidor lo puede hacer la búsqueda en base a los dos valores como esto:

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]]; 
} 
Cuestiones relacionadas