图片轮播控件,自动播放,下一张,上一张,双击缩放,鼠标悬停,功能模块独立,可按需求删减。以下是用例步骤。
第一步:创建UserControl,
第二步:修改命名空间
第三步:修改数据源
CS代码:
using CS.View.Common.SY;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace CS
{
/// <summary>
/// ImgListBoxs.xaml 的交互逻辑
/// </summary>
public partial class ImgListBox : UserControl
{
public ImgListBox()
{
InitializeComponent();
GitImagesList();
RollBox();
}
/// <summary>
/// 计时器
/// </summary>
DispatcherTimer dispatcherTimer = new DispatcherTimer();
public void RollBox()
{
dispatcherTimer.Interval = TimeSpan.FromSeconds(5);
dispatcherTimer.Tick += (object sender, EventArgs e) =>
{
Index++;
};
dispatcherTimer.Start();
}
/// <summary>
/// 给控件添加图片
/// </summary>
public void GitImagesList()
{
//首次添加图片
BitmapImage t = new BitmapImage(new Uri(vs[preindex], UriKind.Relative));
one_img.Source = t;
BitmapImage s = new BitmapImage(new Uri(vs[Index], UriKind.Relative));
two_img.Source = s;
//创建轮播底部按钮
for (int i = 0; i < vs.Length; i++)
{
PART_ListBox.Items.Add(new ListBoxItem());
}
Binding binding = new Binding();
binding.Path = new PropertyPath("Index");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
PART_ListBox.SetBinding(ListBox.SelectedIndexProperty, binding);
}
//数据源
string[] vs = { "/Assets/Picture/Pic1.jpg", "/Assets/Picture/Pic1.jpg", "/Assets/Picture/Pmg3.jpg", "/Assets/Picture/Pmg2.jpg", "/Assets/Picture/Pmg3.jpg" };
Binding imgbing = new Binding("MaxIma");
public List<FrameworkElement> Items { get; set; } = new List<FrameworkElement>();
public event PropertyChangedEventHandler PropertyChanged;
int preindex = 0;
public int _Index { get; set;