/// <summary> /// Adds the child to the FluidElements collection and initializes its RenderTransform. /// </summary> /// <param name="child">UIElement</param> private void AddChildToFluidElements(UIElement child) { // Add the child to the FluidElements collection FluidElements.Add(child); // Initialize its RenderTransform child.RenderTransform = layoutManager.CreateTransform(-ItemWidth, -ItemHeight, NORMAL_SCALE, NORMAL_SCALE); }
/// <summary> /// Intializes the arrangement of the children /// </summary> private void InitializeArrange() { foreach (UIElement child in FluidElements) { // Get the child's index in the FluidElements int index = FluidElements.IndexOf(child); // Get the initial location of the child Point pos = layoutManager.GetInitialLocationOfChild(index); // Initialize the appropriate Render Transform for the child child.RenderTransform = layoutManager.CreateTransform(pos.X, pos.Y, NORMAL_SCALE, NORMAL_SCALE); } }
/// <summary> /// Moves the dragElement to the new Index /// </summary> /// <param name="newIndex">Index of the new location</param> /// <returns>True-if dragElement was moved otherwise False</returns> private bool UpdateDragElementIndex(int newIndex) { // Check if the dragElement is being moved to its current place // If yes, then no need to proceed further. (Improves efficiency!) int dragCellIndex = FluidElements.IndexOf(dragElement); if (dragCellIndex == newIndex) { return(false); } FluidElements.RemoveAt(dragCellIndex); FluidElements.Insert(newIndex, dragElement); return(true); }
private void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (var item in e.OldItems) { FluidElements.Remove((UIElement)item); } } if (e.NewItems != null) { foreach (var item in e.NewItems) { // Check if the child is already added to the FluidElements collection if (!FluidElements.Contains((UIElement)item)) { AddChildToFluidElements((UIElement)item); } } } }
/// <summary> /// Removes all the children from the FluidWrapPanel /// </summary> private void ClearItemsSource() { FluidElements.Clear(); Children.Clear(); }
/// <summary> /// Iterates through all the fluid elements and animate their /// movement to their new location. /// </summary> private void UpdateFluidLayout(bool showEasing = true) { // Iterate through all the fluid elements and animate their // movement to their new location. var validElement = FluidElements.Where(o => o != null && o.Visibility == Visibility.Visible).ToList(); for (int index = 0; index < validElement.Count; index++) { UIElement element = validElement[index]; //if (element == null||element.Visibility==Visibility.Collapsed) // continue; // If an child is currently being dragged, then no need to animate it if (dragElement != null && index == validElement.IndexOf(dragElement)) { continue; } element.Arrange(new Rect(0, 0, element.DesiredSize.Width, element.DesiredSize.Height)); // Get the cell position of the current index Point pos = layoutManager.GetPointFromIndex(index); Storyboard transition; // Is the child being animated the same as the child which was last dragged? if (element == lastDragElement) { if (!showEasing) { // Create the Storyboard for the transition transition = layoutManager.CreateTransition(element, pos, FIRST_TIME_ANIMATION_DURATION, null); } else { // Is easing function specified for the animation? TimeSpan duration = (DragEasing != null) ? DEFAULT_ANIMATION_TIME_WITH_EASING : DEFAULT_ANIMATION_TIME_WITHOUT_EASING; // Create the Storyboard for the transition transition = layoutManager.CreateTransition(element, pos, duration, DragEasing); } // When the user releases the drag child, it's Z-Index is set to 1 so that // during the animation it does not go below other elements. // After the animation has completed set its Z-Index to 0 transition.Completed += (s, e) => { if (lastDragElement != null) { lastDragElement.SetValue(Canvas.ZIndexProperty, 0); lastDragElement = null; } }; } else // It is a non-dragElement { if (!showEasing) { // Create the Storyboard for the transition transition = layoutManager.CreateTransition(element, pos, FIRST_TIME_ANIMATION_DURATION, null); } else { // Is easing function specified for the animation? TimeSpan duration = (ElementEasing != null) ? DEFAULT_ANIMATION_TIME_WITH_EASING : DEFAULT_ANIMATION_TIME_WITHOUT_EASING; // Create the Storyboard for the transition transition = layoutManager.CreateTransition(element, pos, duration, ElementEasing); } } // Start the animation transition.Begin(); } }
/// <summary> /// Override for the Measure Layout Phase /// </summary> /// <param name="availableSize">Available Size</param> /// <returns>Size required by the panel</returns> protected override Size MeasureOverride(Size availableSize) { Size availableItemSize = new Size(Double.PositiveInfinity, Double.PositiveInfinity); double rowWidth = 0.0; double maxRowHeight = 0.0; double colHeight = 0.0; double maxColWidth = 0.0; double totalColumnWidth = 0.0; double totalRowHeight = 0.0; // Iterate through all the UIElements in the Children collection for (int i = 0; i < InternalChildren.Count; i++) { UIElement child = InternalChildren[i]; if (child != null) { // Ask the child how much size it needs child.Measure(availableItemSize); //Check if the child is already added to the FluidElements collection if (!FluidElements.Contains(child)) { AddChildToFluidElements(child); } if (this.Orientation == Orientation.Horizontal) { // Will the child fit in the current row? //if (rowWidth + child.DesiredSize.Width > availableSize.Width) if (rowWidth + this.ItemWidth > availableSize.Width) { // Wrap to next row totalRowHeight += maxRowHeight; // Is the current row width greater than the previous row widths if (rowWidth > totalColumnWidth) { totalColumnWidth = rowWidth; } rowWidth = 0.0; maxRowHeight = 0.0; } //rowWidth += child.DesiredSize.Width; rowWidth += this.ItemWidth; //if (child.DesiredSize.Height > maxRowHeight) // maxRowHeight = child.DesiredSize.Height; if (this.ItemHeight > maxRowHeight) { maxRowHeight = this.ItemHeight; } } else // Vertical Orientation { // Will the child fit in the current column? //if (colHeight + child.DesiredSize.Height > availableSize.Height) if (colHeight + this.ItemHeight > availableSize.Height) { // Wrap to next column totalColumnWidth += maxColWidth; // Is the current column height greater than the previous column heights if (colHeight > totalRowHeight) { totalRowHeight = colHeight; } colHeight = 0.0; maxColWidth = 0.0; } //colHeight += child.DesiredSize.Height; colHeight += this.ItemHeight; //if (child.DesiredSize.Width > maxColWidth) // maxColWidth = child.DesiredSize.Width; if (this.ItemWidth > maxColWidth) { maxColWidth = this.ItemWidth; } } } } List <UIElement> dirtyElements = new List <UIElement>(); foreach (var element in FluidElements) { if (!InternalChildren.Contains(element)) { dirtyElements.Add(element); } } foreach (var item in dirtyElements) { FluidElements.Remove(item); } if (this.Orientation == Orientation.Horizontal) { // Add the height of the last row totalRowHeight += maxRowHeight; // If there is only one row, take its width as the total width if (totalColumnWidth == 0.0) { totalColumnWidth = rowWidth; } } else { // Add the width of the last column totalColumnWidth += maxColWidth; // If there is only one column, take its height as the total height if (totalRowHeight == 0.0) { totalRowHeight = colHeight; } } Size resultSize = new Size(totalColumnWidth, totalRowHeight); return(resultSize); }