Posted By

jiewmeng on 09/04/10


Tagged

WPF


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

wxqiang
Tyster
cybermax


WPF DataBinding & Converters


 / Published in: C#
 

reference: blogs.msdn

  1. // XAML
  2. <Window.Resources>
  3. <local:TwoDecimalConverter x:Key="decConverter" />
  4. </Window.Resources>
  5. ...
  6. <Slider x:Name="slider1" TickFrequency="1" TickPlacement="BottomRight" />
  7. <TextBox Text="{Binding ElementName=slider1, Path=Value, Converter={StaticResource decConverter}}" />
  8.  
  9. // C#
  10. public class TwoDecimalConverter : IValueConverter
  11. {
  12. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  13. {
  14. double val;
  15.  
  16. if (value == null)
  17. return "0";
  18.  
  19. if (!Double.TryParse(value.ToString(), out val))
  20. return "0";
  21.  
  22. return val.ToString("F2");
  23.  
  24. }
  25.  
  26. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }

Report this snippet  

You need to login to post a comment.