Converters.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Data;
  8. using System.Globalization;
  9. using System.Windows;
  10. using System.Collections;
  11. namespace ICSharpCode.WpfDesign.Designer.Converters
  12. {
  13. public class IntFromEnumConverter : IValueConverter
  14. {
  15. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  16. public static readonly IntFromEnumConverter Instance = new IntFromEnumConverter();
  17. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. return (int)value;
  20. }
  21. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  22. {
  23. return Enum.ToObject(targetType, (int)value);
  24. }
  25. }
  26. public class HiddenWhenFalse : IValueConverter
  27. {
  28. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  29. public static readonly HiddenWhenFalse Instance = new HiddenWhenFalse();
  30. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  31. {
  32. return (bool)value ? Visibility.Visible : Visibility.Hidden;
  33. }
  34. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. }
  39. public class CollapsedWhenFalse : IValueConverter
  40. {
  41. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  42. public static readonly CollapsedWhenFalse Instance = new CollapsedWhenFalse();
  43. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  44. {
  45. return (bool)value ? Visibility.Visible : Visibility.Collapsed;
  46. }
  47. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  48. {
  49. throw new NotImplementedException();
  50. }
  51. }
  52. public class LevelConverter : IValueConverter
  53. {
  54. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  55. public static readonly LevelConverter Instance = new LevelConverter();
  56. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  57. {
  58. return new Thickness(2 + 14 * (int)value, 0, 0, 0);
  59. }
  60. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  61. {
  62. throw new NotImplementedException();
  63. }
  64. }
  65. public class CollapsedWhenZero : IValueConverter
  66. {
  67. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  68. public static readonly CollapsedWhenZero Instance = new CollapsedWhenZero();
  69. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  70. {
  71. if (value == null || (int)value == 0) {
  72. return Visibility.Collapsed;
  73. }
  74. return Visibility.Visible;
  75. }
  76. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. }
  81. public class FalseWhenNull : IValueConverter
  82. {
  83. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  84. public static readonly FalseWhenNull Instance = new FalseWhenNull();
  85. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  86. {
  87. return value != null;
  88. }
  89. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. }
  94. public class BoldWhenTrue : IValueConverter
  95. {
  96. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  97. public static readonly BoldWhenTrue Instance = new BoldWhenTrue();
  98. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  99. {
  100. return (bool)value ? FontWeights.Bold : FontWeights.Normal;
  101. }
  102. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. }
  107. // Boxed int throw exception without converter (wpf bug?)
  108. public class DummyConverter : IValueConverter
  109. {
  110. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  111. public static readonly DummyConverter Instance = new DummyConverter();
  112. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  113. {
  114. return value;
  115. }
  116. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  117. {
  118. return value;
  119. }
  120. }
  121. public class FormatDoubleConverter : IValueConverter
  122. {
  123. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "converter is immutable")]
  124. public static readonly FormatDoubleConverter Instance=new FormatDoubleConverter();
  125. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  126. {
  127. return Math.Round((double)value);
  128. }
  129. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. }
  134. }