NullableConverter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
  4. // All other rights reserved.
  5. using System.ComponentModel;
  6. using System.Globalization;
  7. namespace System.Windows.Controls
  8. {
  9. /// <summary>
  10. /// Converts a string or base value to a <see cref="Nullable"/> value.
  11. /// </summary>
  12. /// <typeparam name="T">The type should be value type.</typeparam>
  13. /// <QualityBand>Preview</QualityBand>
  14. public class NullableConverter<T> : TypeConverter where T : struct
  15. {
  16. /// <summary>
  17. /// Returns whether the type converter can convert an object from the
  18. /// specified type to the type of this converter.
  19. /// </summary>
  20. /// <param name="context">An object that provides a format context.
  21. /// </param>
  22. /// <param name="sourceType">The type you want to convert from.</param>
  23. /// <returns>
  24. /// Returns true if this converter can perform the conversion;
  25. /// otherwise, false.
  26. /// </returns>
  27. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  28. {
  29. if (sourceType == typeof(T))
  30. {
  31. return true;
  32. }
  33. else if (sourceType == typeof(string))
  34. {
  35. return true;
  36. }
  37. return false;
  38. }
  39. /// <summary>
  40. /// Returns whether the type converter can convert an object from the
  41. /// specified type to the type of this converter.
  42. /// </summary>
  43. /// <param name="context">An object that provides a format context.
  44. /// </param>
  45. /// <param name="destinationType">The type you want to convert to.
  46. /// </param>
  47. /// <returns>
  48. /// Returns true if this converter can perform the conversion;
  49. /// otherwise, false.
  50. /// </returns>
  51. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  52. {
  53. return (destinationType == typeof(T));
  54. }
  55. /// <summary>
  56. /// Converts from the specified value to the type of this converter.
  57. /// </summary>
  58. /// <param name="context">An object that provides a format context.
  59. /// </param>
  60. /// <param name="culture">The
  61. /// <see cref="T:System.Globalization.CultureInfo"/> to use as the
  62. /// current culture.</param>
  63. /// <param name="value">The value to convert to the type of this
  64. /// converter.</param>
  65. /// <returns>The converted value.</returns>
  66. /// <exception cref="T:System.NotSupportedException">
  67. /// The conversion cannot be performed.
  68. /// </exception>
  69. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  70. {
  71. string stringValue = value as string;
  72. if (value is T)
  73. {
  74. return new Nullable<T>((T)value);
  75. }
  76. else if (string.IsNullOrEmpty(stringValue) || String.Equals(stringValue, "Auto", StringComparison.OrdinalIgnoreCase))
  77. {
  78. return new Nullable<T>();
  79. }
  80. return new Nullable<T>((T)Convert.ChangeType(value, typeof(T), culture));
  81. }
  82. /// <summary>
  83. /// Converts from the specified value to the a specified type from the
  84. /// type of this converter.
  85. /// </summary>
  86. /// <param name="context">An object that provides a format context.
  87. /// </param>
  88. /// <param name="culture">The
  89. /// <see cref="T:System.Globalization.CultureInfo"/> to use as the
  90. /// current culture.</param>
  91. /// <param name="value">The value to convert to the type of this
  92. /// converter.</param>
  93. /// <param name="destinationType">The type of convert the value to
  94. /// .</param>
  95. /// <returns>The converted value.</returns>
  96. /// <exception cref="T:System.NotSupportedException">
  97. /// The conversion cannot be performed.
  98. /// </exception>
  99. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  100. {
  101. if (value == null)
  102. {
  103. return string.Empty;
  104. }
  105. else if (destinationType == typeof(string))
  106. {
  107. return value.ToString();
  108. }
  109. return base.ConvertTo(context, culture, value, destinationType);
  110. }
  111. }
  112. }