首先看效果图,然后给出代码。
每10秒自动切换图片,点击右侧图片列表,左侧自动显示该图片。
for (int i = 0; i < _Max; i++)
{
if (_start >= listSrc.Count)
{
_start = _start % listSrc.Count;
}
Image img = this.pnl2.FindName("img" + (i + 1).ToString()) as Image;
img.Source = new BitmapImage(new Uri(listSrc[_start].src, UriKind.Relative));
if (i == currentImgIndex)
{
img.Effect = new DropShadowEffect();
imgBig.Source = img.Source;
sb_Big.Begin();
txtImg.Text = listSrc[_start].name;
}
else
{
img.Effect = null;
}
_start++;
}
}
/// <summary>
/// 点击向上翻时的逻辑处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void up(object sender, MouseButtonEventArgs e)
{
currentIndex--;
if (currentIndex <= 0)
{
currentIndex = listSrc.Count;
}
LoadImage();
}
/// <summary>
/// 点击向下按钮时的逻辑处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void down(object sender, MouseButtonEventArgs e)
{
currentIndex++;
if (currentIndex >= listSrc.Count)
{
currentIndex = 0;
}
LoadImage();
}
/// <summary>
/// 单击右侧小图时,同时步更换大图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImgClick(object sender, MouseButtonEventArgs e)
{
Image imgSmall = sender as Image;
imgBig.Source = imgSmall.Source;
sb_Big.Begin();
for (int i = 1; i <= 6; i++)
{
Image img = this.pnl2.FindName("img" + i.ToString()) as Image;
if (img == imgSmall)
{
img.Effect = new DropShadowEffect();
currentImgIndex = i-1;//重新保存新的小图阴影索引
}
else
{
img.Effect = null;
}
}
//确定新的currentIndex
for (int i = 0; i < listSrc.Count; i++)
{
if ((imgSmall.Source as BitmapImage).UriSource == new Uri(listSrc[i].src, UriKind.Relative))
{
currentIndex = i;
break;
}
}
txtImg.Text = listSrc[currentIndex].name ;
}
/// <summary>
/// 自定义类
/// </summary>
public class ImageItem
{
public string src { set; get; }
public string name { set; get; }
}
private void imgDown_MouseEnter(object sender, MouseEventArgs e)
{
this._timer.Stop();
}
private void imgDown_MouseLeave(object sender, MouseEventArgs e)
{
this._timer.Start();
}
}
}