WPF La unión a un método con el parámetro dentro HierarchicalDataTemplate
-
21-09-2019 - |
Pregunta
¿Hay alguna forma para enlazar un valor a un bloque de texto que se obtiene de un método. Por ejemplo, yo paso mi objeto Person en el HierarchicalDataTemplate, desde allí se puede acceder a su propiedad Weight. Ahora digamos que quiero conseguir el peso en Marte, que yo llamaría el método InMars que toma un parámetro de int EarthWeight. Ahora earthweight va a cambiar de persona a persona, ¿cómo puede este parámetro puede ajustar cada vez?
Solución
La mejor manera de hacerlo es con un convertidor.
public class WeightOnMarsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// value will be the persons weight
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("This method should never be called");
}
}
A continuación, sólo tiene que configurar la unión.
<l:WeightOnMarsConverter x:key="weightOnMars" /> <-- Add this to the resources
{Binding Path=Weight, Converter={StaticResource weightOnMars}}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow