private void BottomTZNextBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { int newIndex = _selectedIndex < 0 ? (ItemsSources.Count > 0 ? 0 : -1) : (_selectedIndex + 1) % ItemsSources.Count; if (_selectedTZPicItem != null) { _selectedTZPicItem.IsSelected = false; } if (newIndex < 0) { return; } _selectedTZPicItem = ImageListSubCanvas.Children[newIndex] as TZPicItem; (ImageListSubCanvas.Children[newIndex] as TZPicItem).IsSelected = true; }
//更新视图 private void UpdateView() { if (ItemsSources != null) { int length = ItemsSources.Count; //初始化画布 Canvas.SetTop(ImageListSubCanvas, 0); ImageListSubCanvas.Children.Clear(); //循环向画布中添加图片 for (int i = 0; i < length; i++) { //初始化图片 TZPicItem TZPicItem = new TZPicItem { Width = 83, Height = 83 }; TZPicItem.PicImage.Source = ItemsSources[i]; TZPicItem.Tag = i; //设置图片位置 Canvas.SetTop(TZPicItem, i * TZPicItem.Height); //添加图片SelectionChanged事件 TZPicItem.SelectionChanged += (s, e) => { TZPicItem sender = s as TZPicItem; if (!sender.IsSelected) { return; } //更新当前选中图片索引 _selectedIndex = int.Parse(TZPicItem.Tag.ToString()); //让选中的图片移动到中间 // 计算要移动的位移 double offset = (double.Parse(TZPicItem.Tag.ToString()) + 0.5) * TZPicItem.ActualHeight - ImageListCanvas.ActualHeight / 2; offset = offset > 0 ? offset : 0; //动画移动 Storyboard moveStory = new Storyboard(); DoubleAnimation animation = new DoubleAnimation(); animation.To = -offset; animation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 300)); Storyboard.SetTarget(animation, ImageListSubCanvas); Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Top)")); moveStory.Children.Add(animation); moveStory.Begin(); //初始化变换 CompositeTransform trans = ShowImage.RenderTransform as CompositeTransform; trans.Rotation = _rotation = 0; //初始化图片大小 InitShowImageSize(); ShowImage.Source = TZPicItem.PicImage.Source; showImageStory.Begin(); }; //添加图片MouseLeftButtonUp事件 TZPicItem.MouseLeftButtonUp += (s, e) => { if (_selectedTZPicItem != null) { _selectedTZPicItem.IsSelected = false; } _selectedTZPicItem = TZPicItem; TZPicItem.IsSelected = true; TZPicItem.Cursor = Cursors.Hand; }; //把图片添加到画布中 ImageListSubCanvas.Children.Add(TZPicItem); //设置默认选中参数 if (length > 0) { _selectedIndex = 0; _selectedTZPicItem = (ImageListSubCanvas.Children[0] as TZPicItem); _selectedTZPicItem.IsSelected = true; } } } }