question

AlumniComp16LAXMISWAMI-6030 avatar image
0 Votes"
AlumniComp16LAXMISWAMI-6030 asked TimonYang-MSFT commented

Is there a feature to shift item on top in a listview?

I have a ListView which ItemSource is bind to an ObservableCollection. This collection stores a list of some user's chat messages. Now I want to shift that item on top whenever that user receives a new chat message. Likewise in WhatsApp and slack app. So I want to know what is the correct way of doing this. Right now I am removing an item from the list and then adding it to the 0th index but this way sometimes I am getting parameter incorrect issues. So is there any way so that I can directly shift any chat to the top without removing it.

Xaml code for listview is below:

<ListView MaxHeight="{x:Bind ViewModel.OpenedChatMaxHeight, Mode=OneWay}"
Margin="0,10,0,0"
CanDragItems="True"
Loaded="{x:Bind ViewModel.OpenedChatDataLoaded}"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
SelectedIndex="{x:Bind ViewModel.OpenChatListSeletedItem,Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.OpenChatList,Mode=TwoWay}"
SelectionChanged="{x:Bind ViewModel.ChatSelected}"
IsItemClickEnabled="True"
ItemClick="{x:Bind ViewModel.OpenPinnedChatListItemClick}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepItemsInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

When there is a new chat message, how do you add it to the ObservableCollection, using the Add Method?

Try to replace it with Insert Method.

         collection.Insert(0, "New Message");

If you want to move an item that already exists in the collection to the first one, you can try the Move Method.

             collection.Move(collection.Count - 1, 0);

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AlumniComp16LAXMISWAMI-6030
May I know if you have a chance to check my answer? If this answer does not meet your needs, please let me know and we can explore further.

0 Votes 0 ·

I have tried the Move method but in that case, I always get index out of range error my view just gets out of focus. I have to manage ListView explicitly. So I am looking for an alternative way to do this

0 Votes 0 ·
TimonYang-MSFT avatar image TimonYang-MSFT AlumniComp16LAXMISWAMI-6030 ·

When this exception occurs, are there any elements in the ObservableCollection?
Could you please show us some code that caused the exception?

0 Votes 0 ·