PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/StockTrader RI/Silverlight/StockTraderRI.Infrastructure.Silverlight/Converters/EnumToBooleanConverter.Silverlight.cs

#
C# | 61 lines | 38 code | 7 blank | 16 comment | 9 complexity | c19a0f52b0c10d787da66326778b9cf5 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 StockTraderRI.Infrastructure.Converters
  21. {
  22. public class EnumToBooleanConverter : IValueConverter
  23. {
  24. #region IValueConverter Members
  25. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  26. {
  27. var stringParameter = parameter as string;
  28. if (value == null || string.IsNullOrEmpty(stringParameter))
  29. {
  30. return false;
  31. }
  32. return string.Equals(value.ToString(), stringParameter, StringComparison.Ordinal);
  33. }
  34. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  35. {
  36. var stringParameter = parameter as string;
  37. if (value == null || string.IsNullOrEmpty(stringParameter))
  38. {
  39. return null;
  40. }
  41. object parsedParameter = Enum.Parse(targetType, stringParameter, true);
  42. if (parsedParameter == null)
  43. {
  44. return null;
  45. }
  46. if ((bool)value)
  47. {
  48. return parsedParameter;
  49. }
  50. return null;
  51. }
  52. #endregion
  53. }
  54. }