CutOverControl.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Threading;
  9. namespace Muchinfo.WPF.Controls.MarQuees
  10. {
  11. public delegate void OnItemClickHander(Object sender, RoutedEventArgs e);
  12. /// <summary>
  13. /// CutOverControl.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class CutOverControl : UserControl
  16. {
  17. public CutOverControl()
  18. {
  19. InitializeComponent();
  20. _playTimer.Interval = TimeSpan.FromSeconds(2);
  21. _playTimer.Tick += _playTimer_Tick;
  22. this.Height = this.FontFamily.LineSpacing * this.FontSize + _borderThiness;
  23. }
  24. private const double _borderThiness = 2; //显示边框的大小
  25. private DispatcherTimer _playTimer = new DispatcherTimer();
  26. private ScrollViewer _listBoxScrollViewer; //列表的滚动条
  27. /// <summary>
  28. /// 列表的滚动条
  29. /// </summary>
  30. private ScrollViewer ListBoxScrollViewer
  31. {
  32. get
  33. {
  34. if (_listBoxScrollViewer != null)
  35. {
  36. return _listBoxScrollViewer;
  37. }
  38. _listBoxScrollViewer = FindVisualChild<ScrollViewer>(ContentListBox);
  39. return _listBoxScrollViewer;
  40. }
  41. }
  42. /// <summary>
  43. /// 从父容器中查找指定类型的控件
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="parent"></param>
  47. /// <returns></returns>
  48. public T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
  49. {
  50. if (parent == null) return null;
  51. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
  52. {
  53. var child = VisualTreeHelper.GetChild(parent, i);
  54. if (child != null && child is T)
  55. {
  56. return (T)child;
  57. }
  58. T childItem = FindVisualChild<T>(child);
  59. if (childItem != null)
  60. return childItem;
  61. }
  62. return null;
  63. }
  64. //切换另一字符串
  65. private void _playTimer_Tick(object sender, EventArgs e)
  66. {
  67. if (ItemsSource == null || ItemsSource.Count == 0)
  68. {
  69. _playTimer.Stop();
  70. return;
  71. }
  72. if (ListBoxScrollViewer == null) return;
  73. if (ListBoxScrollViewer.VerticalOffset >=
  74. (ListBoxScrollViewer.ExtentHeight -
  75. ListBoxScrollViewer.ViewportHeight))
  76. {
  77. ListBoxScrollViewer.ScrollToTop();
  78. }
  79. else
  80. {
  81. ListBoxScrollViewer.ScrollToVerticalOffset(ListBoxScrollViewer.VerticalOffset +
  82. ListBoxScrollViewer.ViewportHeight);
  83. }
  84. }
  85. /// <summary>
  86. /// 更换时间
  87. /// </summary>
  88. public TimeSpan DelayTime
  89. {
  90. get { return (TimeSpan)GetValue(DelayTimeProperty); }
  91. set { SetValue(DelayTimeProperty, value); }
  92. }
  93. // Using a DependencyProperty as the backing store for DelayTime. This enables animation, styling, binding, etc...
  94. public static readonly DependencyProperty DelayTimeProperty =
  95. DependencyProperty.Register("DelayTime", typeof(TimeSpan), typeof(CutOverControl), new PropertyMetadata(TimeSpan.FromSeconds(1), OnDelayTimeChange));
  96. /// <summary>
  97. /// 显示内容
  98. /// </summary>
  99. public Object DisplayContet
  100. {
  101. get { return (Object)GetValue(DisplayContetProperty); }
  102. set { SetValue(DisplayContetProperty, value); }
  103. }
  104. // Using a DependencyProperty as the backing store for DisplayContet. This enables animation, styling, binding, etc...
  105. public static readonly DependencyProperty DisplayContetProperty =
  106. DependencyProperty.Register("DisplayContet", typeof(Object), typeof(CutOverControl), new PropertyMetadata(null));
  107. /// <summary>
  108. /// 循环显示的数据源
  109. /// </summary>
  110. public IList ItemsSource
  111. {
  112. get { return (IList)GetValue(ItemsSourceProperty); }
  113. set { SetValue(ItemsSourceProperty, value); }
  114. }
  115. // Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
  116. public static readonly DependencyProperty ItemsSourceProperty =
  117. DependencyProperty.Register("ItemsSource", typeof(IList), typeof(CutOverControl), new PropertyMetadata(new List<Object>(), OnItemsSourceChange));
  118. /// <summary>
  119. /// 项模板
  120. /// </summary>
  121. public DataTemplate ItemTemplate
  122. {
  123. get { return (DataTemplate)GetValue(ItemTemplateProperty); }
  124. set { SetValue(ItemTemplateProperty, value); }
  125. }
  126. // Using a DependencyProperty as the backing store for ItemTemplate. This enables animation, styling, binding, etc...
  127. public static readonly DependencyProperty ItemTemplateProperty =
  128. DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(CutOverControl), new PropertyMetadata(null));
  129. //数据源改变时
  130. private static void OnItemsSourceChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  131. {
  132. var cutOver = d as CutOverControl;
  133. if (cutOver == null) return;
  134. if (e.NewValue is IList)
  135. {
  136. var listView = e.NewValue as IList;
  137. if (listView.Count > 0)
  138. {
  139. cutOver._playTimer.Start();
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 当更换时间改变时
  145. /// </summary>
  146. private static void OnDelayTimeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  147. {
  148. var cutOver = d as CutOverControl;
  149. if (cutOver == null) return;
  150. cutOver._playTimer.Stop();
  151. cutOver._playTimer.Interval = cutOver.DelayTime;
  152. cutOver._playTimer.Start(); //重新开始
  153. }
  154. //当鼠标放在上面时停止滚动
  155. private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
  156. {
  157. if (_playTimer.IsEnabled)
  158. {
  159. _playTimer.Stop();
  160. }
  161. }
  162. //当鼠标放在离开时滚动
  163. private void UIElement_OnMouseLeave(object sender, MouseEventArgs e)
  164. {
  165. if (!_playTimer.IsEnabled)
  166. {
  167. _playTimer.Start();
  168. }
  169. }
  170. public event OnItemClickHander ItemClick = null;
  171. /// <summary>
  172. /// 选择项
  173. /// </summary>
  174. private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
  175. {
  176. // throw new NotImplementedException();
  177. if (ItemClick != null)
  178. {
  179. ItemClick(sender, e);
  180. }
  181. }
  182. }
  183. }