code_junkie

WPF DataBinding with simple arithmatic operation?

Nov 11th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=5}"/>
  2. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=8}"/>
  3. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myInt, Constant=24}"/>
  4.  
  5. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus5}"/>
  6. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus8}"/>
  7. <TextBox Canvas.Top="{Binding ElementName=mySource, Path=myIntPlus24}"/>
  8.  
  9. public class AddValueConverter : IValueConverter
  10. {
  11. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  12. {
  13. object result = value;
  14. int parameterValue;
  15.  
  16. if (value != null && targetType == typeof(Int32) &&
  17. int.TryParse((string)parameter,
  18. NumberStyles.Integer, culture, out parameterValue))
  19. {
  20. result = (int)value + (int)parameterValue;
  21. }
  22.  
  23. return result;
  24. }
  25.  
  26. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31.  
  32. <Setter Property="Grid.ColumnSpan"
  33. Value="{Binding
  34. Path=ColumnDefinitions.Count,
  35. RelativeSource={RelativeSource AncestorType=Grid},
  36. Converter={StaticResource addValueConverter},
  37. ConverterParameter=1}"
  38. />
  39.  
  40. <TextBox Canvas.Top="{Binding SomeValue,
  41. Converter={StaticResource MathConverter},
  42. ConverterParameter=@VALUE+5}" />
  43.  
  44. Width="{Binding ElementName=RootWindow, Path=ActualWidth,
  45. Converter={StaticResource MathConverter},
  46. ConverterParameter=((@VALUE-200)*.3)}"
Add Comment
Please, Sign In to add comment