Cree un convertidor que implemente IMultiValueConverter. Podría ser algo como esto:
class AverageConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int total = 0;
int number = 0;
foreach (object o in values)
{
int i;
bool parsed = int.TryParse(o.ToString(), out i);
if (parsed)
{
total += i;
number++;
}
}
if (number == 0) return 0;
return (total/number).ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Un convertidor de valores múltiples recibe una matriz de objetos, uno para cada uno de los enlaces. Puede procesarlos de la forma que necesite, dependiendo de si tiene la intención de duplicarlo o int o lo que sea.
Si las dos cajas de texto están unidas por datos, puede usar los mismos enlaces en la multibinding para su bloque de texto (recordando notificar cuando la propiedad cambia para que se actualice su promedio), o puede obtener el valor del texto refiriéndose al cuadros de texto por ElementName.
<TextBox Text="{Binding Value1}" x:Name="TextBox1" />
<TextBox Text="{Binding Value2}" x:Name="TextBox2" />
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource AverageConverter}">
<Binding ElementName="TextBox1" Path="Text" />
<Binding ElementName="TextBox2" Path="Text" />
<!-- OR -->
<!-- <Binding Path="Value1" /> -->
<!-- <Binding Path="Value2" /> -->
</MultiBinding>
</TextBlock.Text>
</TextBlock>
En caso de que desee pasar un valor de constancia a uno de los enlaces de multi-binding, ¿cómo puedo hacerlo? ¡Por favor ayuda! –
@Jacob: Por error presiono el botón de votar y ahora me permite revertirlo y dice: "No puedo hasta la próxima edición" :-( –