CommandParameter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. namespace OpenSEMI.ClientBase.Command
  7. {
  8. public class CommandParameter<TCustomParameter>
  9. {
  10. public TCustomParameter CustomParameter { get; private set; }
  11. protected CommandParameter(TCustomParameter customParameter)
  12. {
  13. this.CustomParameter = customParameter;
  14. }
  15. public static CommandParameter<TCustomParameter> Cast(object parameter)
  16. {
  17. var parameterToCast = parameter as CommandParameter<object>;
  18. if (parameterToCast == null)
  19. {
  20. throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
  21. parameter.GetType(), typeof(CommandParameter<object>)));
  22. }
  23. var castedParameter = new CommandParameter<TCustomParameter>(
  24. (TCustomParameter)parameterToCast.CustomParameter);
  25. return castedParameter;
  26. }
  27. }
  28. public class EventCommandParameter<TCustomParameter, TEventArgs> : CommandParameter<TCustomParameter>
  29. where TEventArgs : RoutedEventArgs
  30. {
  31. public RoutedEvent RoutedEvent { get; private set; }
  32. public TEventArgs EventArgs { get; private set; }
  33. public EventCommandParameter(
  34. TCustomParameter customParameter,
  35. RoutedEvent routedEvent,
  36. TEventArgs eventArgs)
  37. : base(customParameter)
  38. {
  39. this.RoutedEvent = routedEvent;
  40. this.EventArgs = eventArgs;
  41. }
  42. public static new EventCommandParameter<TCustomParameter, TEventArgs> Cast(object parameter)
  43. {
  44. var parameterToCast = parameter as EventCommandParameter<object, RoutedEventArgs>;
  45. if (parameterToCast == null)
  46. {
  47. throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
  48. parameter.GetType(), typeof(EventCommandParameter<object, RoutedEventArgs>)));
  49. }
  50. var castedParameter = new EventCommandParameter<TCustomParameter, TEventArgs>(
  51. (TCustomParameter)parameterToCast.CustomParameter,
  52. parameterToCast.RoutedEvent,
  53. (TEventArgs)parameterToCast.EventArgs);
  54. return castedParameter;
  55. }
  56. }
  57. public class PropertyCommandParameter<TCustomParameter, TValue> : CommandParameter<TCustomParameter>
  58. {
  59. public DependencyProperty Property { get; private set; }
  60. public TValue Value { get; private set; }
  61. public PropertyCommandParameter(
  62. TCustomParameter customParameter,
  63. DependencyProperty property,
  64. TValue value)
  65. : base(customParameter)
  66. {
  67. this.Property = property;
  68. this.Value = value;
  69. }
  70. public static new PropertyCommandParameter<TCustomParameter, TValue> Cast(object parameter)
  71. {
  72. var parameterToCast = parameter as PropertyCommandParameter<object, object>;
  73. if (parameterToCast == null)
  74. {
  75. throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
  76. parameter.GetType(), typeof(PropertyCommandParameter<object, object>)));
  77. }
  78. var castedParameter = new PropertyCommandParameter<TCustomParameter, TValue>(
  79. (TCustomParameter)parameterToCast.CustomParameter,
  80. parameterToCast.Property,
  81. (TValue)parameterToCast.Value);
  82. return castedParameter;
  83. }
  84. }
  85. }