Nous avons vu ensemble comment créer un value converter lors d'un précédent post.
Si vous avez un jour besoin de faire un calcul, tel qu'une division dans une interface Xaml.
Par exemple, Binder la taille d'un élément sur un autre, mais en la divisant par 20.
Vous pourrez vous en sortir avec un ValueConverter, voyez celui-ci :
C# :
class SimpleRatioConverter : IValueConverter
{
private double ratio = 5; public double Ratio
{
get { return ratio; }
set { ratio = value; }
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (double)value * ratio;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (double)value / ratio;
}
}
Xaml :
...
<Page.Resources>
<MyApp:SimpleRatioConverter x:Key="mySRC" Ratio="0.05"/>
</Page.Resources>
...
<UnUIElement Width="{Binding ElementName=UnAutreUIElement, Path=ActualWidth, Converter={StaticResource mySRC}}"/>
...