public CaretAdorner(TextBox adornedElement) : base(adornedElement) { var fontRect = adornedElement.GetRectFromCharacterIndex(0); this.caret = new Rectangle { Width = 1, Height = fontRect.Height, Fill = Brushes.Black, RenderTransform = new TranslateTransform(fontRect.Left, fontRect.Top), Opacity = 0, SnapsToDevicePixels = true }; adornedElement.SizeChanged += OnSizeChanged; adornedElement.SelectionChanged += OnSelectionChanged; adornedElement.PreviewMouseDown += OnPreviewMouseDown; adornedElement.GotKeyboardFocus += OnGotFocus; adornedElement.LostKeyboardFocus += OnLostFocus; this.children = new VisualCollection(this); this.children.Add(this.caret); double transitionTime = SystemInformation.CaretBlinkTime; TimeSpan fadeTime = TimeSpan.FromMilliseconds(transitionTime * (1d / 3d)); TimeSpan holdTime = TimeSpan.FromMilliseconds(transitionTime * (2d / 3d)); var storyboard = new Storyboard { RepeatBehavior = RepeatBehavior.Forever }; var fade = new DoubleAnimationUsingKeyFrames { KeyFrames = { new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(fadeTime)), new DiscreteDoubleKeyFrame(0, KeyTime.FromTimeSpan(fadeTime + holdTime)), new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(fadeTime + holdTime + fadeTime)), new DiscreteDoubleKeyFrame(1, KeyTime.FromTimeSpan(fadeTime + holdTime + fadeTime + holdTime)) }, Duration = new Duration(TimeSpan.FromMilliseconds(transitionTime * 2)) }; Storyboard.SetTargetProperty(fade, new PropertyPath("Opacity")); fade.Freeze(); storyboard.Children.Add(fade); this.blinkStoryboard = storyboard; }
private void AssociatedObjectOnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems == null || e.AddedItems.Count < 1 || !AssociatedObject.LayoutPaths.Any()) { return; } LayoutPath target = AssociatedObject.LayoutPaths.First(); int selectedIndex = AssociatedObject.SelectedIndex; int itemCount = AssociatedObject.Items.Count; // アニメーションする移動量を計算する。 double from; double to = DefaultStart - MaximumLength / itemCount * selectedIndex; target.Start = to; if (_previousSelectedIndex == 0 && selectedIndex == itemCount - 1) { from = DefaultStart - MaximumLength; } else if (_previousSelectedIndex == itemCount - 1 && selectedIndex == 0) { from = DefaultStart + MaximumLength / itemCount; } else { from = target.Start; } // Storyboardを作成する。 var frames = new DoubleAnimationUsingKeyFrames(); frames.KeyFrames.Add(new EasingDoubleKeyFrame(from, KeyTime.FromTimeSpan(TimeSpan.Zero))); frames.KeyFrames.Add(new EasingDoubleKeyFrame(to, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)))); Storyboard.SetTarget(frames, target); Storyboard.SetTargetProperty(frames, new PropertyPath(LayoutPath.StartProperty)); frames.Freeze(); var storyboard = new Storyboard(); storyboard.Children.Add(frames); storyboard.Freeze(); AssociatedObject.BeginStoryboard(storyboard); _previousSelectedIndex = selectedIndex; }
private void SetAnimation() { storyBoard?.Stop(Selected); if ((EdgesPath?.Count ?? 0) == 0) { return; } storyBoard = new Storyboard(); DoubleAnimationUsingKeyFrames leftAnimation = new DoubleAnimationUsingKeyFrames(); DoubleAnimationUsingKeyFrames topAnimation = new DoubleAnimationUsingKeyFrames(); leftAnimation.RepeatBehavior = RepeatBehavior.Forever; topAnimation.RepeatBehavior = RepeatBehavior.Forever; Storyboard.SetTargetName(leftAnimation, Selected.Name); Storyboard.SetTargetProperty(leftAnimation, new PropertyPath("(0)", Canvas.LeftProperty)); Storyboard.SetTargetName(topAnimation, Selected.Name); Storyboard.SetTargetProperty(topAnimation, new PropertyPath("(0)", Canvas.TopProperty)); double sumDist = EdgesPath.Sum(edge => Distance(edge)); double sumTime = 0; foreach (var edge in EdgesPath) { leftAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(Nodes[edge.Begin].X, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, (int)sumTime)))); topAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(Nodes[edge.Begin].Y, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, (int)sumTime)))); sumTime += (Distance(edge) / sumDist) * (animTimeSpan * EdgesPath.Count); leftAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(Nodes[edge.End].X, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, (int)sumTime)))); topAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(Nodes[edge.End].Y, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, (int)sumTime)))); } leftAnimation.Freeze(); topAnimation.Freeze(); storyBoard.Children.Add(leftAnimation); storyBoard.Children.Add(topAnimation); storyBoard.Begin(Selected, true); }