Un ValueConverter pour calculer

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}}"/>
...

 

Posted on 01/09/2007 01:49:00 by Togis

Permalink | Commentaires (0) | Post RSSRSS comment feed |

Categories: WPF | Tips | Binding

Tags:

Actuellement noté 2.0 par 2 personne(s)

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Billets liés

Ajouter un commentaire


(Affichera votre icône Gravatar)  

  Country flag





Live preview

novembre 21. 2008 19:10