PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/V2.2/trunk/Quickstarts/Commanding/Desktop/Commanding.OrderModule/Converter/StringToNullableNumberConverter.cs

#
C# | 57 lines | 35 code | 6 blank | 16 comment | 8 complexity | 761ec7def1a6db69e0563d2a5838773f MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Globalization;
  19. using System.Windows.Data;
  20. namespace Commanding.Modules.Order.Converter
  21. {
  22. public class StringToNullableNumberConverter : IValueConverter
  23. {
  24. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  25. {
  26. return value;
  27. }
  28. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  29. {
  30. string stringValue = value as string;
  31. if (stringValue != null)
  32. {
  33. if (targetType == typeof(int?))
  34. {
  35. int result;
  36. if (int.TryParse(stringValue, out result))
  37. return result;
  38. return null;
  39. }
  40. if (targetType == typeof(decimal?))
  41. {
  42. decimal result;
  43. if (decimal.TryParse(stringValue, out result))
  44. return result;
  45. return null;
  46. }
  47. }
  48. return value;
  49. }
  50. }
  51. }