Windows 8.1 XAML ListView Programmatic Reorder - c#

With a list view containing items I can reorder which gives a nice UI effect.
I have my item source for the list view hooked up to an observable collection. Is there anyway with the framework I could programatically simulate a reorder and give the same effect you would have if you actually reordering?
The only way I've been able to slightly achieve this is to literally clear all the items from my data source then repopulate after shuffling the items, however it doesn't look nice.

Well, you could sort the items in code-behind in a temporary helper array and then according to the order they are in in the sorted one start periodically moving items one-by-one in a small time interval (with DispatcherTimer) withing the collection - remove at it's index and insert somewhere it belongs. I think there is no built-in way to do it in a simpler manner.

Related

Is sorting using CollectionView performance efficient?

I have a ICollectionView which serves as an input source for a WPF ListView. The number of items (text messages) in the CollectionView could be upto 10 thousands. I want to add a sorting creiteria to the collection view based on the TimeStamp. The latest added message should be on top.
MyCollectionView.SortDescriptions.Add(new SortDescription("TimeStamp", ListSortDirection.Descending));
Question: If I use the above sorting criterion, does the sorting takes place every time I add a new message? Or does the CollectionView maintains a sorted list internally and in my scenerio (i.e. having TimeStamp as Sorting), it will only need to compare the new incoming message's TimeStamp with the last added message's TimeStamp?
You'd do better NOT relying on collectionview sorting and NOT presenting 10,000 items to a listview.
The answer:
By default, when you’re using a CollectionView(Source) to do sorting, grouping and filtering in an itemscontrol, the sorting/grouping/filtering behavior will update when you explicitly refresh CollectionView or
when you add or remove an item to the collection.
You should therefore not sort the collection. I think your best bet is likely to instead insert at position zero of an un sorted observablecollection.
If you're inserting lots of items quickly then that's still going to burn through cycles. These are on your UI thread so you best not plan on anyone trying to interact with your UI. If these messages come in fast enough you will just have a blur anyhow.
I think you should probably instead only show the user the latest few messages in a "live" view. Don't let them sort.
If they need to see 10,000 then put that in another view that shows them a static snapshot. See what your performance is like and think about iterating when you have something concrete to play round with.

How to animate Silverlight 4 ListBox items when the bound data is sorted?

I'm trying to find out exactly what happens when the data bound with ItemsSource changes. So which method should I override to be able to make the items animate to their new positions in the sorted list?
From what I found out so far I might have to hijack the LayoutUpdated of the ScrollViewer inside the ListBox? How do I find out what happens there in order to be able to attach an animation to the ListBoxItems' position assignments?
Actually it would be even nicer to do this with some attached triggers and behaviors and not having to extend the ListBox class.
Edit: as it turned out both PagedCollectionView and CollectionViewSource destroys and recreates the item list on sorting, so rearranging the list with an animation doesn't seem to be possible without either implementing sorting manually by moving existing items around in the list or to create some sort of item buffer, both of which sounding like a possible source of weird bugs and performance bottleneck. If anyone has a working solution to these though I'd be glad to get any pointers!

Sorting a ListBox in WPF

Let me start off by saying that I am completely new with WPF (this is my first project and I have been working in it for less than a week). With that being said, please be easy on me!
I have three list ListBoxes that are being bound to ObservableCollections from a LINQ queries. In the beginning, everything is fine, all three are populated correctly. My client needs to drag and drop selections from one ListBox to another. I also have this working, but when I do the drag and drop, the new selection is placed at the bottom of the ListBox instead of being sorted alphabetically with the existing items.
How can I sort the ListBox at runtime through code behind after the drag and drop operation is complete.
Thanks!
It is not entirely clear how you handle drag and drop in your code. You say that your ListBoxes are all data-bound - which implies that you actually move items from one backing collection to another on drag and drop. If so, ListBox just displays the items in order they are present in the collection. You should either sort them there, or, if sorting is a view-only behavior in your case (i.e. items are actually unordered in data model, by design), you should use CollectionView to wrap your collections, set it up to do the sorting, and bind the ListBoxes to that.

C#: What is the best way to implement a 'filter' mechanism for ListView Items?

C#: What is the best way to implement a 'filter' mechanism for ListView Items?
I want to filter out some listview items from view in 'details' mode where it contains rows and columns. What would be the best option to create such a mechanism where I could rapidly remove the items in question from view, leaving the others intact, and putting them back into the listview when there is no more need to filter out listview items? Should I remove/copy them to a List and just and add them back when done or would there be a better method of doing this more effeciently? The listview will be handeling about 100-500 items.
If you are working with a databound control you will have this facility within the binding framework.
If not, I would probably store all the items for the list separately and populate the control in full each time, based on any contextual requirements such as filtering. The code to iterate through the list and move items not required at present is probably unnecessarily complicated. A full repopulate each time will be easier and won't differ much in terms of computational expense.
This behavior is built in to BindingSources using DataSets in .Net 2.0.
For .Net 3.0+, you can use LINQ.

How to efficiently filter a large LIstViewItemCollection?

So I have a ListView with an upper limit of about 1000 items. I need to be able to filter these items using a textbox's TextChanged event. I have some code that works well for a smaller number of items (~400), but when I need to re-display a full list of all 1000 items, it takes about 4 seconds.
I am not creating new ListViewItems every time. Instead, I keep a list of the full item collection and then add from that. It seems that the .Add method is taking a long time regardless. Here is a little sample:
this.BeginUpdate();
foreach (ListViewItem item in m_cachedItems)
{
MyListView.Add(item);
}
this.EndUpdate;
I have tried only adding the missing items (i.e., the difference between the items currently being displayed and the total list of items), but this doesn't work either. There can be a situation in which there is only one item currently displayed, the user clears the textbox, and I need to display the entire list.
I am not very experienced in eeking performance out of .NET controls with a large sample like this, so I don't really know a better way to do it. Is there any way around using the .Add() method, or if not, just e better general solution?
There is a better way, you can use the VirtualMode of the list view.
That documentation should get you started. The idea is to provide information to the ListView only as it's needed. Such information is retrieved using events. All you have to do is implement those events and tell the list view how many items it contains.
AddRange is much faster than add
MyListView.AddRange(items)
There are two things to address this:
Turn off sorting while manipulating the list contents.
Hide the list so it doesn't try to paint.
The 1st point is the biggest performance gain in list manipulation out of these two. To achieve this, just set the ListViewItemSorter to null for the duration of the modification and set it back at the end.
For the 2nd option, I often draw the list to a bitmap and then show that bitmap in a PictureBox so the user doesn't see the list disappear, then just reshow the list when I'm done.
Also note that you can hide items and so make them invisible without removing them. So add all your items the first time around and then later on you just hide the ones no longer needed and show the ones that are.

Categories

Resources