| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Threading;
- namespace Muchinfo.WPF.Controls.MarQuees
- {
- public delegate void OnItemClickHander(Object sender, RoutedEventArgs e);
- /// <summary>
- /// CutOverControl.xaml 的交互逻辑
- /// </summary>
- public partial class CutOverControl : UserControl
- {
- public CutOverControl()
- {
- InitializeComponent();
- _playTimer.Interval = TimeSpan.FromSeconds(2);
- _playTimer.Tick += _playTimer_Tick;
- this.Height = this.FontFamily.LineSpacing * this.FontSize + _borderThiness;
- }
- private const double _borderThiness = 2; //显示边框的大小
- private DispatcherTimer _playTimer = new DispatcherTimer();
- private ScrollViewer _listBoxScrollViewer; //列表的滚动条
- /// <summary>
- /// 列表的滚动条
- /// </summary>
- private ScrollViewer ListBoxScrollViewer
- {
- get
- {
- if (_listBoxScrollViewer != null)
- {
- return _listBoxScrollViewer;
- }
- _listBoxScrollViewer = FindVisualChild<ScrollViewer>(ContentListBox);
- return _listBoxScrollViewer;
- }
- }
- /// <summary>
- /// 从父容器中查找指定类型的控件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="parent"></param>
- /// <returns></returns>
- public T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
- {
- if (parent == null) return null;
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- if (child != null && child is T)
- {
- return (T)child;
- }
- T childItem = FindVisualChild<T>(child);
- if (childItem != null)
- return childItem;
- }
- return null;
- }
- //切换另一字符串
- private void _playTimer_Tick(object sender, EventArgs e)
- {
- if (ItemsSource == null || ItemsSource.Count == 0)
- {
- _playTimer.Stop();
- return;
- }
- if (ListBoxScrollViewer == null) return;
- if (ListBoxScrollViewer.VerticalOffset >=
- (ListBoxScrollViewer.ExtentHeight -
- ListBoxScrollViewer.ViewportHeight))
- {
- ListBoxScrollViewer.ScrollToTop();
- }
- else
- {
- ListBoxScrollViewer.ScrollToVerticalOffset(ListBoxScrollViewer.VerticalOffset +
- ListBoxScrollViewer.ViewportHeight);
- }
- }
- /// <summary>
- /// 更换时间
- /// </summary>
- public TimeSpan DelayTime
- {
- get { return (TimeSpan)GetValue(DelayTimeProperty); }
- set { SetValue(DelayTimeProperty, value); }
- }
- // Using a DependencyProperty as the backing store for DelayTime. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty DelayTimeProperty =
- DependencyProperty.Register("DelayTime", typeof(TimeSpan), typeof(CutOverControl), new PropertyMetadata(TimeSpan.FromSeconds(1), OnDelayTimeChange));
- /// <summary>
- /// 显示内容
- /// </summary>
- public Object DisplayContet
- {
- get { return (Object)GetValue(DisplayContetProperty); }
- set { SetValue(DisplayContetProperty, value); }
- }
- // Using a DependencyProperty as the backing store for DisplayContet. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty DisplayContetProperty =
- DependencyProperty.Register("DisplayContet", typeof(Object), typeof(CutOverControl), new PropertyMetadata(null));
- /// <summary>
- /// 循环显示的数据源
- /// </summary>
- public IList ItemsSource
- {
- get { return (IList)GetValue(ItemsSourceProperty); }
- set { SetValue(ItemsSourceProperty, value); }
- }
- // Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ItemsSourceProperty =
- DependencyProperty.Register("ItemsSource", typeof(IList), typeof(CutOverControl), new PropertyMetadata(new List<Object>(), OnItemsSourceChange));
- /// <summary>
- /// 项模板
- /// </summary>
- public DataTemplate ItemTemplate
- {
- get { return (DataTemplate)GetValue(ItemTemplateProperty); }
- set { SetValue(ItemTemplateProperty, value); }
- }
- // Using a DependencyProperty as the backing store for ItemTemplate. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ItemTemplateProperty =
- DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(CutOverControl), new PropertyMetadata(null));
- //数据源改变时
- private static void OnItemsSourceChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var cutOver = d as CutOverControl;
- if (cutOver == null) return;
- if (e.NewValue is IList)
- {
- var listView = e.NewValue as IList;
- if (listView.Count > 0)
- {
- cutOver._playTimer.Start();
- }
- }
- }
- /// <summary>
- /// 当更换时间改变时
- /// </summary>
- private static void OnDelayTimeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var cutOver = d as CutOverControl;
- if (cutOver == null) return;
- cutOver._playTimer.Stop();
- cutOver._playTimer.Interval = cutOver.DelayTime;
- cutOver._playTimer.Start(); //重新开始
- }
- //当鼠标放在上面时停止滚动
- private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
- {
- if (_playTimer.IsEnabled)
- {
- _playTimer.Stop();
- }
- }
- //当鼠标放在离开时滚动
- private void UIElement_OnMouseLeave(object sender, MouseEventArgs e)
- {
- if (!_playTimer.IsEnabled)
- {
- _playTimer.Start();
- }
- }
- public event OnItemClickHander ItemClick = null;
- /// <summary>
- /// 选择项
- /// </summary>
- private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
- {
- // throw new NotImplementedException();
- if (ItemClick != null)
- {
- ItemClick(sender, e);
- }
- }
- }
- }
|