WPF DataBinding & Converters


/ Published in: C#
Save to your folder(s)

reference: [blogs.msdn](http://blogs.msdn.com/b/bencon/archive/2006/05/10/594886.aspx)


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.