locked
How to get listview selectedindex landing (insertion point) after reorder?

    Question

  • It's a 8.1 store app and after I allow the user to reorder the listview items I need to know where selectedindex landed.

    I.e. listview has 5 items (0-4 indexes), selectedindex is 2 (so item 3 in list).

    I allow drag, reorder, etc. The user moves item 5 (index 4) to front of line (position 0). My selectedindex was 2 now it became 3 since item (5) was inserted before it.

    How do I get that value? Drop event doesn't fire. There isnt insertion mark, etc.

    Ideally I would like an event to fire after move (drop) and I can find where it was dropped?, etc.

    This should be that hard, but I can't find much on Google, etc.

    vs2013, C#

    Thanx.

    Thursday, December 18, 2014 3:33 PM

All replies

  • Ok, I've tried this and it appears to work.

    My listview has an underlying collection (myList). So I use that event to update position of song playing if someone moves list item before current playing song.

    Thoughts?

    void MyList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
      {
       int startingPos = e.OldStartingIndex;
       int endingPos = e.NewStartingIndex;
       int currentPosition = lvMusicList.SelectedIndex;
       if (endingPos != -1 & currentPosition != -1)
       {
        if ((endingPos < currentPosition) | (endingPos == currentPosition))//move place
        {
         CurrentSongPosition += 1;
        }
        else if ((endingPos > currentPosition) | (endingPos == currentPosition))//move place
        {
         CurrentSongPosition -= 1;
        }

       }
       
      }


    • Edited by chrislynn Thursday, December 18, 2014 9:28 PM
    • Proposed as answer by Bill ChungMVP Saturday, December 20, 2014 7:34 AM
    • Unproposed as answer by Bill ChungMVP Saturday, December 20, 2014 7:34 AM
    Thursday, December 18, 2014 7:06 PM
  • Hi Chrislynn,

    Thank you for sharing your solution.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, December 19, 2014 6:24 AM
    Moderator
  • The only issue with my code is that it is not covering all scenarios apparently. But I'm still reviewing.

    I can't believe that there just isn't a "dropindex" or event for this? "drop" won't work, and I can't find an event that will. The automatic sorting is great, instead of coding that the old way.

    Saturday, December 20, 2014 5:01 PM
  • Hi Chrislynn,

    I found a similar question about this problem. I suggest you try the method from http://stackoverflow.com/questions/16388867/xaml-c-what-event-fires-after-reordering-a-gridview.

    In CollectionChanged event, you can set the SelectedIndex property of ListView control.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, December 26, 2014 8:38 AM
    Moderator
  • Thanx, but not really the same. You don't have to "manage" the reordering on the listview since that can be automatic. The issue is that there is not an on drop completion fire event. There is however on the underlying collection.

    ObservableCollection<Song> MyList = new ObservableCollection<Song>();

    And this is pretty close so far, I'm sure there is a better way.

    void MyList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    		{
    			int OldStartingIndex = e.OldStartingIndex;
    			int NewStartingIndex = e.NewStartingIndex;
    			int SelectedSong = lvMusicList.SelectedIndex;
    
    			System.Diagnostics.Debug.WriteLine("OldStartingIndex:" + OldStartingIndex + " NewStartingIndex: " + NewStartingIndex + " SelectedSong: " + SelectedSong + " CurrentSong + CurrentSongPosition);
    
    			if (CurrentSongPosition >= 0) //songs have been loaded for playing
    			{
    				SongPositions.Add(new SongPosition() { NewPosition = NewStartingIndex, OldPosition = OldStartingIndex });
    
    				if (isDeleting) //check for deleteing first
    				{
    
    					System.Diagnostics.Debug.WriteLine("Deleting - OldStartingIndex:" + OldStartingIndex + " NewStartingIndex: " + NewStartingIndex + " SelectedSong: " + SelectedSong + " CurrentSong + CurrentSongPosition);
    					//clear out songs
    					SongPositions.Clear();
    					isDeleting = false;
    
    					return;
    				}
    
    				if (SongPositions.Count > 1) //we have an ending drop location
    				{
    
    					//compare
    					if (SongPositions[0].OldPosition == CurrentSongPosition) //moving selected song
    					{
    
    						lvMusicList.SelectedIndex = SongPositions[1].NewPosition;
    						CurrentSongPosition = SongPositions[1].NewPosition;
    
    					}
    					else if ((SongPositions[0].OldPosition == -1) & (SongPositions[1].NewPosition > CurrentSongPosition)) //new music added to playlist
    					{
    						//no movement
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    
    					}
    					else if ((SongPositions[0].OldPosition < CurrentSongPosition) & (SongPositions[1].NewPosition < CurrentSongPosition)) //grabbed below and moved below
    					{
    						//no movement
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    					}
    					else if ((SongPositions[0].OldPosition > CurrentSongPosition) & (SongPositions[1].NewPosition > CurrentSongPosition)) //grabbed above and moved below
    					{
    						//no movement
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    					}
    					else if ((SongPositions[0].OldPosition > CurrentSongPosition) & (SongPositions[1].NewPosition == CurrentSongPosition)) //grabbed above and moved equal to currentplay position
    					{
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    						CurrentSongPosition += 1;
    					}
    					else if ((SongPositions[0].OldPosition < CurrentSongPosition) & (SongPositions[1].NewPosition >= CurrentSongPosition)) //grabbed below, moving playing back one
    					{
    						CurrentSongPosition -= 1;
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    
    					}
    					else  //grabbed and droppped below, moving playing up one
    					{
    						lvMusicList.SelectedIndex = CurrentSongPosition;
    						CurrentSongPosition += 1;
    
    					}
    
    					//clear out songs
    					SongPositions.Clear();
    					System.Diagnostics.Debug.WriteLine("After change - OldStartingIndex:" + OldStartingIndex + " NewStartingIndex: " + NewStartingIndex + " SelectedSong: " + SelectedSong + " CurrentSong + CurrentSongPosition);
    
    					//update play count
    					updateSongPlayingCount();
    				}
    
    			}
    
    			//System.Diagnostics.Debug.WriteLine("OldStartingIndex:" + OldStartingIndex + " NewStartingIndex: " + NewStartingIndex + " SelectedSong: " + SelectedSong + " CurrentSong + CurrentSongPosition);
    
    		}

     
    Thursday, January 01, 2015 9:12 PM