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