private void AyTreeViewItemCollapsed(object sender, RoutedEventArgs e) { TreeViewItem tvi = e.OriginalSource as TreeViewItem; //tvi.IsExpanded = false; if (tvi != null && tvi.Items.Count > 0) { itemhost = WpfTreeHelper.GetChildObject <ItemsPresenter>(tvi, "ItemsHost"); // double targetHeight = 0; ////添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; if (temp.RelativeItem != null) { temp.ParentCategory.IsExpanded = false; break; } } if (itemhost != null) { itemhost.Visibility = Visibility.Visible; DoubleAnimationUsingKeyFrames HeightKey = new DoubleAnimationUsingKeyFrames(); HeightKey.Duration = new Duration(TimeSpan.FromMilliseconds(ExpandedTime)); HeightKey.FillBehavior = FillBehavior.Stop; EasingDoubleKeyFrame HeightKey0 = new EasingDoubleKeyFrame(itemhost.ActualHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(0); HeightKey1.EasingFunction = easyOut; HeightKey.KeyFrames.Add(HeightKey0); HeightKey.KeyFrames.Add(HeightKey1); HeightKey.Completed += HeightKey_Completed; itemhost.BeginAnimation(ItemsPresenter.HeightProperty, HeightKey); } } e.Handled = true; }
/// <summary> /// 每个时常都是一样的,暂定200 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AyTreeViewItemExpanded(object sender, RoutedEventArgs e) { TreeViewItem tvi = e.OriginalSource as TreeViewItem; if (tvi != null && tvi.Items.Count > 0) { int executeTime = 0;//执行time误差 int durationTime = 300; double targetHeight = 0; int expandNode = 0; //添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; temp.ParentCategory.IsExpanded = true; if (temp.RelativeItem == null) { temp.RelativeItem = TreeViewHelper.GetTreeViewItem2(tvi, temp); } expandNode += GetTreeViewItemCount(temp); if (IsNeedMoveAndOpacity) { if (temp.RelativeItem != null) { temp.RelativeItem.Opacity = 0; } } } targetHeight += (TreeViewItemHeight + OtherBorderTreeViewHeight) * expandNode;//计算当前展开的有多少个item //set itemhost的高度 var itemhost = WpfTreeHelper.GetChildObject <ItemsPresenter>(tvi, "ItemsHost"); if (itemhost != null) { itemhost.Visibility = Visibility.Visible; DoubleAnimationUsingKeyFrames HeightKey = new DoubleAnimationUsingKeyFrames(); HeightKey.FillBehavior = FillBehavior.Stop; EasingDoubleKeyFrame HeightKey0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); HeightKey.KeyFrames.Add(HeightKey0); if (IsNeedMoveAndOpacity) { EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(targetHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(ExpandedTime)), easyOut); HeightKey.KeyFrames.Add(HeightKey1); } else { EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(targetHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(ExpandedTime))); HeightKey.KeyFrames.Add(HeightKey1); } itemhost.BeginAnimation(ItemsPresenter.HeightProperty, null); itemhost.BeginAnimation(ItemsPresenter.HeightProperty, HeightKey); } if (IsNeedMoveAndOpacity) { int index = 0; int endIndex = tvi.Items.Count; //添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; if (temp.RelativeItem != null) { index++; DoubleAnimationUsingKeyFrames OpacityKey = new DoubleAnimationUsingKeyFrames(); EasingDoubleKeyFrame opacityKey10 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); EasingDoubleKeyFrame opacityKey0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime))); EasingDoubleKeyFrame opacityKey1 = new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime + durationTime))); opacityKey1.EasingFunction = easyIn; OpacityKey.KeyFrames.Add(opacityKey0); OpacityKey.KeyFrames.Add(opacityKey1); ThicknessAnimationUsingKeyFrames MarginKey = new ThicknessAnimationUsingKeyFrames(); var tempThickness = GetTargetThickness(temp.Depth); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(new Thickness(0, 0, 0, 0), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)))); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(new Thickness(0, 0, 0, 0), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime)))); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(tempThickness, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime + durationTime)))); opacityKey1.EasingFunction = easyIn; temp.RelativeItem.BeginAnimation(TreeViewItem.OpacityProperty, null); temp.RelativeItem.BeginAnimation(TreeViewItem.OpacityProperty, OpacityKey); temp.RelativeItem.BeginAnimation(TreeViewItem.PaddingProperty, null); temp.RelativeItem.BeginAnimation(TreeViewItem.PaddingProperty, MarginKey); OpacityKey = null; MarginKey = null; executeTime += 30; if (endIndex == index) { temp.RelativeItem.BringIntoView(); } } } executeTime = 0; } } e.Handled = true; }