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); /// /// CutOverControl.xaml 的交互逻辑 /// 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; //列表的滚动条 /// /// 列表的滚动条 /// private ScrollViewer ListBoxScrollViewer { get { if (_listBoxScrollViewer != null) { return _listBoxScrollViewer; } _listBoxScrollViewer = FindVisualChild(ContentListBox); return _listBoxScrollViewer; } } /// /// 从父容器中查找指定类型的控件 /// /// /// /// public T FindVisualChild(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(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); } } /// /// 更换时间 /// 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)); /// /// 显示内容 /// 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)); /// /// 循环显示的数据源 /// 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(), OnItemsSourceChange)); /// /// 项模板 /// 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(); } } } /// /// 当更换时间改变时 /// 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; /// /// 选择项 /// private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e) { // throw new NotImplementedException(); if (ItemClick != null) { ItemClick(sender, e); } } } }