public void DemoteLayer(LayerCollection owningCollection, int position)
        {
            // Skip if the layer can't be moved because its already at the bottom.
            if (position == owningCollection.Count - 1)
            {
                return;
            }

            // Move the layer by removing it from its current position and inserting it at the next higher position.
            Layer selectedLayer = owningCollection[position];

            owningCollection.RemoveAt(position);
            owningCollection.Insert(position + 1, selectedLayer);
        }
        public void PromoteLayer(LayerCollection owningCollection, int position)
        {
            // Skip if the layer can't be moved up because it is already at the top.
            if (position < 1)
            {
                return;
            }

            // Move the layer by removing it from its current position and adding it at the next lower position.
            Layer selectedLayer = owningCollection[position];

            owningCollection.RemoveAt(position);
            owningCollection.Insert(position - 1, selectedLayer);
        }
        public override void MoveRow(UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
        {
            // Find the source and destination lists (based on the section of the source and destination index).
            LayerCollection source      = sourceIndexPath.Section == 0 ? IncludedLayers : ExcludedLayers;
            LayerCollection destination = destinationIndexPath.Section == 0 ? IncludedLayers : ExcludedLayers;

            // Find the layer that is being moved.
            Layer movedLayer = source[sourceIndexPath.Row];

            // Remove the layer from the source list and insert into the destination list.
            source.RemoveAt(sourceIndexPath.Row);
            destination.Insert(destinationIndexPath.Row, movedLayer);

            // Reload the table now that the data has changed.
            tableView.ReloadData();
        }
        private void ListBoxItem_OnDrop(object sender, DragEventArgs e)
        {
            // This method is called when the user finishes dragging while over the listbox.

            // Find the source and destination list boxes.
            ListBox sourceBox = FindParentListBox(_originatingListBoxItem);

            if (sourceBox == null)
            {
                // Return if the source isn't valid - happens when duplicate events are raised.
                return;
            }

            // Find the list box that the item was dropped on (i.e. dragged to).
            ListBox destinationBox = FindParentListBox((UIElement)sender);

            // Get the data that is being dropped.
            Layer draggedData = (Layer)e.Data.GetData(typeof(ArcGISMapImageLayer));

            // Find where in the respective lists the items are.
            int indexOfRemoved = sourceBox.Items.IndexOf(draggedData);
            int indexOfInsertion;

            // Sender is the control that the item is being dropped on. Could be a listbox or a listbox item.
            if (sender is ListBoxItem)
            {
                // Find the layer that the item represents.
                Layer targetData = ((ListBoxItem)sender).DataContext as Layer;

                // Find the position of the layer in the listbox.
                indexOfInsertion = destinationBox.Items.IndexOf(targetData);
            }
            else if (destinationBox != sourceBox)
            {
                // Drop the item at the end of the list if the user let go of the item on the empty space in the box rather than the list item.
                // This works because both the listbox and its individual listbox items participate in drag and drop.
                indexOfInsertion = destinationBox.Items.Count - 1;
            }
            else
            {
                return;
            }

            // Find the appropriate source and destination boxes.
            LayerCollection sourceList      = sourceBox == IncludedListBox ? _viewModel.IncludedLayers : _viewModel.ExcludedLayers;
            LayerCollection destinationList = destinationBox == IncludedListBox ? _viewModel.IncludedLayers : _viewModel.ExcludedLayers;

            // Return if there is nothing to do.
            if (sourceList == destinationList && indexOfRemoved == indexOfInsertion)
            {
                return;
            }

            if (sourceBox == destinationBox && indexOfRemoved < indexOfInsertion)
            {
                indexOfInsertion -= 1;
            }

            // Perform the move.
            sourceList.RemoveAt(indexOfRemoved);
            destinationList.Insert(indexOfInsertion + 1, draggedData);
        }