Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:04:01+00:00 2026-05-23T11:04:01+00:00

I recently asked a question on how to reorder an ItemsControl using Drag and

  • 0

I recently asked a question on how to reorder an ItemsControl using Drag and Drop (ItemsControl Drag and Drop). The answer worked great for the time being (code below) but now I am trying to implement MVVM and the current solution requires access to items in the view. Any ideas how to change this to work with MVVM? I plan to make attached properties to bind to commands but I don’t know how to get rid of lines such as: int index = (int)(e.GetPosition(DimsContainer).X / width);

Current drag and drop code:

private void DimsContainer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _isDown = true;
        _startPoint = e.GetPosition(this.DimsContainer);
    }

    private void DimsContainer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _isDown = false;
        _isDragging = false;

        if (_realDragSource != null)
        {
            _realDragSource.ReleaseMouseCapture();
        }
    }

    private void DimsContainer_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (_isDown)
        {
            if ((_isDragging == false) &&
                ((Math.Abs(e.GetPosition(this.DimsContainer).X - _startPoint.X) >
                    SystemParameters.MinimumHorizontalDragDistance) ||
                 (Math.Abs(e.GetPosition(this.DimsContainer).Y - _startPoint.Y) >
                    SystemParameters.MinimumVerticalDragDistance)))
            {
                _isDragging = true;
                _realDragSource = e.Source as UIElement;
                _realDragSource.CaptureMouse();

                double width = ((FrameworkElement)(this.DimsContainer.ItemContainerGenerator.ContainerFromIndex(0))).ActualWidth;
                int index = (int)(e.GetPosition(DimsContainer).X / width);
                DragDrop.DoDragDrop(_realDragSource, new DataObject("UIElement", index, true), DragDropEffects.Move);
            }
        }
    }

    private void DimsContainer_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("UIElement"))
        {
            e.Effects = DragDropEffects.Move;
        }
    }

    private void DimsContainer_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("UIElement"))
        {
            int sourceIndex = (int)e.Data.GetData("UIElement");
            int removedObject = this.indices[sourceIndex];
            this.indices.RemoveAt(sourceIndex);

            UIElement droptarget = e.Source as UIElement;

            // If a drag/drop is happening, there is definitely
            // one child in the DimsContainer
            double width = ((FrameworkElement)(this.DimsContainer.ItemContainerGenerator.ContainerFromIndex(0))).ActualWidth;
            int index = (int)(e.GetPosition(DimsContainer).X / width);

            try
            {
                this.indices.Insert(index, removedObject);
            }
            catch (InvalidOperationException)
            {
                // ignore
            }

            _isDown = false;
            _isDragging = false;
            _realDragSource.ReleaseMouseCapture();
        }
    }

    public static int RemoveItemFromItemsControl(ItemsControl itemsControl, object itemToRemove)
    {
        int indexToBeRemoved = -1;
        if (itemToRemove != null)
        {
            indexToBeRemoved = itemsControl.Items.IndexOf(itemToRemove);

            if (indexToBeRemoved != -1)
            {
                IEnumerable itemsSource = itemsControl.ItemsSource;
                if (itemsSource == null)
                {
                    itemsControl.Items.RemoveAt(indexToBeRemoved);
                }
                // Is the ItemsSource IList or IList<T>? If so, remove the item from the list.
                else if (itemsSource is IList)
                {
                    ((IList)itemsSource).RemoveAt(indexToBeRemoved);
                }
                else
                {
                    Type type = itemsSource.GetType();
                    Type genericIListType = type.GetInterface("IList`1");
                    if (genericIListType != null)
                    {
                        type.GetMethod("RemoveAt").Invoke(itemsSource, new object[] { indexToBeRemoved });
                    }
                }
            }
        }
        return indexToBeRemoved;
    }

    public static void InsertItemInItemsControl(ItemsControl itemsControl, object itemToInsert, int insertionIndex)
    {
        if (itemToInsert != null)
        {
            IEnumerable itemsSource = itemsControl.ItemsSource;

            if (itemsSource == null)
            {
                itemsControl.Items.Insert(insertionIndex, itemToInsert);
            }
            // Is the ItemsSource IList or IList<T>? If so, insert the dragged item in the list.
            else if (itemsSource is IList)
            {
                ((IList)itemsSource).Insert(insertionIndex, itemToInsert);
            }
            else
            {
                Type type = itemsSource.GetType();
                Type genericIListType = type.GetInterface("IList`1");
                if (genericIListType != null)
                {
                    type.GetMethod("Insert").Invoke(itemsSource, new object[] { insertionIndex, itemToInsert });
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:04:02+00:00Added an answer on May 23, 2026 at 11:04 am

    use drag and drop behavior. e.g. http://www.codeproject.com/KB/WPF/gong-wpf-dragdrop-ii.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked this question and got back a great solution using jquery for
I recently asked a question about Self-Joins and I got a great answer. The
I recently asked a question about formatting JavaScript code in Vim. And I've also
Recently I was asked this question in an interview. I gave an answer in
I recently asked this question here and got some great answers! Custom SQL GROUP
I recently asked a question and posted some code, to which a suggestion was
I recently asked a question here, and someone provided this answer: private void button1_Click(object
I recently asked a question about using the Page.User or HttpContext.Current.User in the View.
I recently asked a question about using Fluent NHibernate with .NET 4 - I
I've recently asked a question regarding IE8 issue. After some time the problem was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.