PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/toolkit/PhoneToolkitSample/Extensions.cs

https://bitbucket.org/jeremejevs/word-steps
C# | 159 lines | 112 code | 29 blank | 18 comment | 13 complexity | f29bc161c08507278630e8cbc34fbf7f MD5 | raw file
  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;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. namespace PhoneToolkitSample
  12. {
  13. public static class Extensions
  14. {
  15. /// <summary>
  16. /// Return a random item from a list.
  17. /// </summary>
  18. /// <typeparam name="T">The item type.</typeparam>
  19. /// <param name="rnd">The Random instance.</param>
  20. /// <param name="list">The list to choose from.</param>
  21. /// <returns>A randomly selected item from the list.</returns>
  22. public static T Next<T>(this Random rnd, IList<T> list)
  23. {
  24. return list[rnd.Next(list.Count)];
  25. }
  26. }
  27. /// <summary>
  28. /// A class used to expose the Key property on a dynamically-created Linq grouping.
  29. /// The grouping will be generated as an internal class, so the Key property will not
  30. /// otherwise be available to databind.
  31. /// </summary>
  32. /// <typeparam name="TKey">The type of the key.</typeparam>
  33. /// <typeparam name="TElement">The type of the items.</typeparam>
  34. public class PublicGrouping<TKey, TElement> : IGrouping<TKey, TElement>
  35. {
  36. private readonly IGrouping<TKey, TElement> _internalGrouping;
  37. public PublicGrouping(IGrouping<TKey, TElement> internalGrouping)
  38. {
  39. _internalGrouping = internalGrouping;
  40. }
  41. public override bool Equals(object obj)
  42. {
  43. PublicGrouping<TKey, TElement> that = obj as PublicGrouping<TKey, TElement>;
  44. return (that != null) && (this.Key.Equals(that.Key));
  45. }
  46. public override int GetHashCode()
  47. {
  48. return Key.GetHashCode();
  49. }
  50. #region IGrouping<TKey,TElement> Members
  51. public TKey Key
  52. {
  53. get { return _internalGrouping.Key; }
  54. }
  55. #endregion
  56. #region IEnumerable<TElement> Members
  57. public IEnumerator<TElement> GetEnumerator()
  58. {
  59. return _internalGrouping.GetEnumerator();
  60. }
  61. #endregion
  62. #region IEnumerable Members
  63. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  64. {
  65. return _internalGrouping.GetEnumerator();
  66. }
  67. #endregion
  68. }
  69. public class CommandButton : Button
  70. {
  71. public ICommand Command
  72. {
  73. get { return (ICommand)GetValue(CommandProperty); }
  74. set { SetValue(CommandProperty, value); }
  75. }
  76. public static readonly DependencyProperty CommandProperty =
  77. DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandButton), new PropertyMetadata(OnCommandChanged));
  78. private static void OnCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  79. {
  80. ((CommandButton)obj).OnCommandChanged(e);
  81. }
  82. public object CommandParameter
  83. {
  84. get { return (object)GetValue(CommandParameterProperty); }
  85. set { SetValue(CommandParameterProperty, value); }
  86. }
  87. public static readonly DependencyProperty CommandParameterProperty =
  88. DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandButton), new PropertyMetadata(OnCommandParameterChanged));
  89. private static void OnCommandParameterChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  90. {
  91. ((CommandButton)obj).UpdateIsEnabled();
  92. }
  93. private void OnCommandChanged(DependencyPropertyChangedEventArgs e)
  94. {
  95. if (e.OldValue != null)
  96. {
  97. ICommand command = e.OldValue as ICommand;
  98. if (command != null)
  99. {
  100. command.CanExecuteChanged -= CommandCanExecuteChanged;
  101. }
  102. }
  103. if (e.NewValue != null)
  104. {
  105. ICommand command = e.NewValue as ICommand;
  106. if (command != null)
  107. {
  108. command.CanExecuteChanged += CommandCanExecuteChanged;
  109. }
  110. }
  111. UpdateIsEnabled();
  112. }
  113. private void CommandCanExecuteChanged(object sender, EventArgs e)
  114. {
  115. UpdateIsEnabled();
  116. }
  117. private void UpdateIsEnabled()
  118. {
  119. IsEnabled = Command != null ? Command.CanExecute(CommandParameter) : false;
  120. }
  121. protected override void OnClick()
  122. {
  123. base.OnClick();
  124. if (Command != null)
  125. {
  126. Command.Execute(CommandParameter);
  127. }
  128. }
  129. }
  130. }