DraggableWindowHeader.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using MuchInfo.Chart.Infrastructure.Helpers;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace MuchInfo.Chart.Infrastructure.Controls
  8. {
  9. /// <summary>
  10. /// DraggableWindowHeader.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class DraggableWindowHeader : UserControl
  13. {
  14. #region Fields
  15. private Point _currentPoint;
  16. private Popup _popup;
  17. private Point _popupPoint;
  18. #endregion Fields
  19. #region Constructors
  20. public DraggableWindowHeader()
  21. {
  22. this.InitializeComponent();
  23. }
  24. #endregion Constructors
  25. #region Delegates
  26. public delegate void DeleteClickedEventHandler();
  27. #endregion Delegates
  28. #region Events
  29. public event DraggableWindowHeader.DeleteClickedEventHandler DeleteClicked;
  30. #endregion Events
  31. #region Properties
  32. #region Public Properties
  33. public CornerRadius CornerRadius
  34. {
  35. get
  36. {
  37. return this.borderMain.CornerRadius;
  38. }
  39. set
  40. {
  41. this.borderMain.CornerRadius = value;
  42. }
  43. }
  44. public Brush DeleteButtonForeColor
  45. {
  46. get
  47. {
  48. return this.borderDelete.BorderBrush;
  49. }
  50. set
  51. {
  52. this.borderDelete.BorderBrush = value;
  53. this.txtDelete.Foreground = value;
  54. }
  55. }
  56. public Brush HeaderBackGround
  57. {
  58. get
  59. {
  60. return this.borderMain.Background;
  61. }
  62. set
  63. {
  64. this.borderMain.Background = value;
  65. }
  66. }
  67. public Brush HeaderBorderColor
  68. {
  69. get
  70. {
  71. return this.borderMain.BorderBrush;
  72. }
  73. set
  74. {
  75. this.borderMain.BorderBrush = value;
  76. }
  77. }
  78. public double HeaderBorderThickness
  79. {
  80. get
  81. {
  82. return this.borderMain.BorderThickness.Left;
  83. }
  84. set
  85. {
  86. this.borderMain.BorderThickness = new Thickness(value, value, value, 0.0);
  87. }
  88. }
  89. public ImageSource HeaderImage
  90. {
  91. get
  92. {
  93. return this.headerIcon.Source;
  94. }
  95. set
  96. {
  97. bool flag = value == null;
  98. if (flag)
  99. {
  100. this.headerIcon.Visibility = Visibility.Collapsed;
  101. }
  102. else
  103. {
  104. this.headerIcon.Visibility = Visibility.Visible;
  105. }
  106. this.headerIcon.Source = value;
  107. }
  108. }
  109. public Panel RootPanel { get; set; }
  110. public string HeaderText
  111. {
  112. get
  113. {
  114. return this.txtHeader.Text;
  115. }
  116. set
  117. {
  118. this.txtHeader.Text = value;
  119. }
  120. }
  121. public bool ShowDeleteButton
  122. {
  123. get
  124. {
  125. return this.borderDelete.Visibility == 0;
  126. }
  127. set
  128. {
  129. this.borderDelete.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  130. }
  131. }
  132. #endregion Public Properties
  133. #endregion Properties
  134. #region Methods
  135. #region Protected Methods
  136. protected virtual void OnDeleteClicked()
  137. {
  138. var handler = DeleteClicked;
  139. if (handler != null) handler();
  140. }
  141. #endregion Protected Methods
  142. #region Private Methods
  143. private void borderDelete_MouseEnter(object sender, MouseEventArgs e)
  144. {
  145. this.txtDelete.Foreground = new SolidColorBrush(ColorHelper.Firebrick);
  146. }
  147. private void borderDelete_MouseLeave(object sender, MouseEventArgs e)
  148. {
  149. this.txtDelete.Foreground = this.borderDelete.BorderBrush;
  150. }
  151. private void borderDelete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  152. {
  153. OnDeleteClicked();
  154. }
  155. private void borderGrab_LostMouseCapture(object sender, MouseEventArgs e)
  156. {
  157. this._popup = null;
  158. }
  159. private void borderGrab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  160. {
  161. this._popup = this.GetPopup(this);
  162. bool flag = this._popup != null;
  163. if (flag)
  164. {
  165. this._currentPoint = e.GetPosition(RootPanel);
  166. this._popupPoint = new Point(this._popup.HorizontalOffset, this._popup.VerticalOffset);
  167. this.borderGrab.CaptureMouse();
  168. }
  169. }
  170. private void borderGrab_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  171. {
  172. bool flag = this._popup != null;
  173. if (flag)
  174. {
  175. this.ShowPopup(e);
  176. this._popup = null;
  177. this.borderGrab.ReleaseMouseCapture();
  178. }
  179. }
  180. private void borderGrab_MouseMove(object sender, MouseEventArgs e)
  181. {
  182. bool flag = this._popup != null;
  183. if (flag)
  184. {
  185. this.ShowPopup(e);
  186. }
  187. }
  188. private Popup GetPopup(FrameworkElement aControl)
  189. {
  190. bool flag = aControl.Parent != null && aControl.Parent is Popup;
  191. Popup result;
  192. if (flag)
  193. {
  194. var popup = (Popup)aControl.Parent;
  195. result = popup;
  196. }
  197. else
  198. {
  199. flag = (aControl.Parent != null && aControl.Parent is FrameworkElement);
  200. result = flag ? this.GetPopup((FrameworkElement)aControl.Parent) : null;
  201. }
  202. return result;
  203. }
  204. private void ShowPopup(MouseEventArgs e)
  205. {
  206. var position = e.GetPosition(RootPanel);
  207. var point = new Point(position.X - this._currentPoint.X + this._popupPoint.X, position.Y - this._currentPoint.Y + this._popupPoint.Y);
  208. this._popup.VerticalOffset = (point.Y);
  209. this._popup.HorizontalOffset = (point.X);
  210. GeometryHelper.EnsureControlInApplication(this._popup, RootPanel);
  211. }
  212. #endregion Private Methods
  213. #endregion Methods
  214. }
  215. }