<Canvas x:Name="canMain" Background="Transparent" Cursor="Hand" >
<Canvas.Clip>
<!-- 定一个循环滚动的区域 -->
<RectangleGeometry x:Name="myRectangleGeometry" RadiusX="0" RadiusY="0" Rect="0,0, 340,50" />
</Canvas.Clip>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded" >
<BeginStoryboard>
<!-- Canvas.Top :说明从下到上或者从上到下滚动 , RepeatBehavior="Forever" 为循环方式-->
<Storyboard x:Name="animation" Storyboard.TargetProperty="(Canvas.Top)" RepeatBehavior="Forever" >
<DoubleAnimation x:Name="daOne" Storyboard.TargetName="spScrollTwo" From="0" To="0" Duration="0:0:10" />
<DoubleAnimation x:Name="daTwo" Storyboard.TargetName="spScrollOne" From="0" To="0" Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<StackPanel x:Name="spScrollOne" Orientation="Vertical"></StackPanel> >//要滚动的集合一
<StackPanel x:Name="spScrollTwo" Orientation="Vertical"></StackPanel>//要滚动的集合二
</Canvas>
// 这里我采用的是后台动态赋值:
public partial class testScroll: UserControl
{
// 信息条数
int icount = 0;
// 信息显示高度
Double iRoadInforMation = 0;
public testScroll()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(testScroll_Loaded);
}
#region 页面初始化方法
/// <summary>
/// 页面初始化方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void testScroll_Loaded(object sender, RoutedEventArgs e)
{
//动态获取数据
getDataInfor();
DispatcherTimer dt;
//定义计时器
dt = new DispatcherTimer()
{
Interval = TimeSpan.FromMinutes(3)
};
dt.Tick += (sen, sec) =>
{
getDataInfor();
};
dt.Start();
}
#endregion
#region 获得信息
/// <summary>
/// 获得信息
/// </summary>
private void getDataInfor()
{
...... ......... // 连接数据库
// 返回成功信息
if (r.IsSucceed)
{
// 定义实体类的集合
List<R_Infor> _Data_InforList = r.Body.FromJsonString<List<R_Infor>>();
// 判断返回查询结果
if (null != _Data_InforList && 0 < _Data_InforList .Count)
{
// 根据查询信息给页面赋值
foreach (var _Infor in_Data_InforList )
{
double _fontsize = 15;
++icount;
// 定义页面滚动显示信息容器
var tbInforOne = new TextBlock();
// 设定显示信息的样式信息
tbInforOne .Text = icount + ". " + _Infor .nayiyaou(); //描述内容
tbInforOne .Tag =Infor .ID();//内容ID
tbInforOne .FontSize = _fontsize;
tbInforOne .LineHeight = _fontsize;
tbInforOne .TextWrapping = TextWrapping.Wrap; //自动换行
tbInforOne .Margin = new Thickness(3, 4, 3, 3); //位置
tbInforOne .Width = (double)325; //显示宽度
spScrollOne.Children.Add(tbTraInforOne); //显示添加上前台控件
iRoadInforMation += (tbInforOne .ActualHeight + 7); //获得实际显示高度
// 定义页面滚动显示信息容器
var tbInforTwo = new TextBlock();
tbInforTwo .Text = icount + ". " + _Infor .nayiyaou(); //描述内容
tbInforTwo .Tag =Infor .ID();//内容ID
tbInforTwo .FontSize = _fontsize;
tbInforTwo .LineHeight = _fontsize;
tbInforTwo .TextWrapping = TextWrapping.Wrap; //自动换行
tbInforOne .Margin = new Thickness(3, 4, 3, 3); //位置
tbInforTwo .Width = (double)325; //显示宽度
spScrollTwo.Children.Add(tbTraInforTwo);
}
setScrolltt();
}
}
};
_Do.Post();
}
#endregion
#region 设置滚动信息
/// <summary>
/// 设置滚动信息
/// </summary>
private void setScrolltt()
{
iRoadInforMation = iRoadInforMation + 10;
myRectangleGeometry.Rect = new Rect(0, 0, 340, iRoadInforMation);
// 控制滚动信息速度
int _marqueeTimeInSeconds = Convert.ToInt32(icount * 1.5);
spScrollOne.Height = iRoadInforMation;
spScrollTwo.Height = iRoadInforMation;
daOne.To = -iRoadInforMation;
daOne.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
daTwo.From = iRoadInforMation;
daTwo.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
animation.Begin();
// 判断是否定制滚动信息
if (canMain.ActualHeight >= spScrollOne.Height)
{
animation.Stop();
return;
}
}
#endregion
困扰了我几天我和大家分享下.