private void ShowItems() { if (this.enteringItem != null) { this.enteringItem.Opacity = 1; this.enteringItem = null; } }
private void ScrollSelected() { PathListBox pathListBox = this.AssociatedObject as PathListBox; if (pathListBox == null) { return; } PathListBoxItem newItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromItem(pathListBox.SelectedItem); // find the item on the path that is closest to the position PathListBoxItem closestItem = null; PathListBoxItem pathListBoxItem; for (int i = 0; i < pathListBox.Items.Count; i++) { pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromIndex(i); if (pathListBoxItem != null && pathListBoxItem.IsArranged) { if (closestItem == null) { closestItem = pathListBoxItem; } else if (Math.Abs(pathListBoxItem.LocalOffset - this.DesiredOffset) < Math.Abs(closestItem.LocalOffset - this.DesiredOffset)) { closestItem = pathListBoxItem; } } } if (closestItem == null || newItem == null || !newItem.IsArranged || !closestItem.IsArranged) { return; } int increment = 0; if (newItem.GlobalOffset < closestItem.GlobalOffset && newItem.GlobalIndex > closestItem.GlobalIndex) { increment = -(pathListBox.Items.Count - newItem.GlobalIndex + closestItem.GlobalIndex); } else if (newItem.GlobalOffset > closestItem.GlobalOffset && newItem.GlobalIndex < closestItem.GlobalIndex) { increment = (pathListBox.Items.Count - closestItem.GlobalIndex + newItem.GlobalIndex); } else { increment = newItem.GlobalIndex - closestItem.GlobalIndex; } bool hideEnteringItem = this.HideEnteringItem; this.HideEnteringItem = false; Scroll(increment); this.HideEnteringItem = hideEnteringItem; }
/// <summary> /// Finds the item that is laid out at the end of the specified layout path.</summary> /// <param name=”layoutPathIndex”> The specified layout path.</param> /// <returns> The global index of the item.</returns> public static int GetLastArrangedIndex(this PathListBox pathListBox, int layoutPathIndex) { if (pathListBox == null) { return(0); } PathListBoxItem pathListBoxItem; PathListBoxItem lastItemArranged = null; int lastArrangedIndex = 0; for (int i = 0; i < pathListBox.Items.Count; i++) { pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromIndex(i); if (pathListBoxItem != null && pathListBoxItem.LayoutPathIndex == layoutPathIndex) { if (lastItemArranged == null) { if (pathListBoxItem.IsArranged) { lastItemArranged = pathListBoxItem; lastArrangedIndex = lastItemArranged.GlobalIndex; } } else { if (pathListBoxItem.LocalIndex > lastItemArranged.LocalIndex && pathListBoxItem.Visibility == Visibility.Visible && pathListBoxItem.IsArranged) { lastItemArranged = pathListBoxItem; lastArrangedIndex = lastItemArranged.GlobalIndex; } } } } return(lastArrangedIndex); }
private void AnimateStart(PathListBox pathListBox, int increment, int layoutPathIndex, int startItemIndex, Duration newDuration, bool isStoryboardInterruped) { LayoutPath layoutPath = pathListBox.LayoutPaths[layoutPathIndex]; if (layoutPath == null || increment == 0) { return; } // Get the distance between the two elements that are being arranged to compute the distance to animate start. int itemCount = pathListBox.Items.Count; int firstIndex = 0; int nextIndex = 0; firstIndex = PathListBoxExtensions.GetFirstArrangedIndex(pathListBox, layoutPathIndex); nextIndex = (firstIndex + 1) % itemCount; PathListBoxItem plbiStart = (PathListBoxItem)(pathListBox.ItemContainerGenerator.ContainerFromIndex(firstIndex)); PathListBoxItem plbiEnd = (PathListBoxItem)(pathListBox.ItemContainerGenerator.ContainerFromIndex(nextIndex)); if (plbiStart.IsArranged == false || plbiEnd.IsArranged == false) { if (pathListBox.WrapItems) { while (plbiStart.IsArranged == false || plbiEnd.IsArranged == false) { firstIndex = (firstIndex + 1) % itemCount; nextIndex = (nextIndex + 1) % itemCount; plbiStart = (PathListBoxItem)(pathListBox.ItemContainerGenerator.ContainerFromIndex(firstIndex)); plbiEnd = (PathListBoxItem)(pathListBox.ItemContainerGenerator.ContainerFromIndex(nextIndex)); } } else { return; } } double startDx = 0; if (plbiStart.LocalOffset > plbiEnd.LocalOffset) { // if the Start and End cross 0 startDx = increment * ((1 - plbiStart.LocalOffset) + plbiEnd.LocalOffset); } else { startDx = increment * (plbiEnd.LocalOffset - plbiStart.LocalOffset); } double startFrom = layoutPath.Start; double startTo = layoutPath.Start; PropertyPath propertyPath = new PropertyPath(String.Format("(ec:PathListBox.LayoutPaths)[{0}].(ec:LayoutPath.Start)", layoutPathIndex)); DoubleAnimation startPropertyAnimation = null; if (isStoryboardInterruped) { // Find the DoubleAnimation that has already been created for this LayoutPath for (int i = 0; i < this.startPropertyStoryboard.Children.Count; i++) { DoubleAnimation currentAnimation = (DoubleAnimation)this.startPropertyStoryboard.Children[i]; PropertyPath pp = Storyboard.GetTargetProperty(currentAnimation); if (currentAnimation != null && String.Equals(pp.Path, propertyPath.Path)) { startPropertyAnimation = currentAnimation; break; } } startTo = (double)startPropertyAnimation.To; ShowItems(); startFrom = startTo; } else { startPropertyAnimation = new DoubleAnimation(); Storyboard.SetTarget(startPropertyAnimation, pathListBox); Storyboard.SetTargetProperty(startPropertyAnimation, propertyPath); this.startPropertyStoryboard.Children.Add(startPropertyAnimation); } startFrom += startDx; int EnteringItemIndex = 0; if (increment > 0) { EnteringItemIndex = pathListBox.GetLastArrangedIndex(layoutPathIndex); EnteringItemIndex = (EnteringItemIndex + 1) % itemCount; } else { EnteringItemIndex = (pathListBox.GetFirstArrangedIndex(layoutPathIndex) - 1) % itemCount; if (EnteringItemIndex < 0) { if (pathListBox.WrapItems) { EnteringItemIndex += itemCount; } } } PathListBoxItem pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromIndex(EnteringItemIndex); if (pathListBoxItem != null && this.HideEnteringItem) { this.enteringItem = pathListBoxItem; this.enteringItem.Opacity = 0; } startPropertyAnimation.Duration = newDuration; startPropertyAnimation.From = startFrom + 0.0001; startPropertyAnimation.To = startTo; startPropertyAnimation.EasingFunction = this.Ease; }