I am using the MVVM pattern and I would like to sort the elements on a datagrid. In my view model I have an ObservableCollection with the element for the datagrid.
In the datagrid I can sort element cliking in the column name, so that is nice, and for multiple columns. I like this feature.
However, I would like to do some more specific ordering, for example show first the element which the ColumnB has "valueC" and later the rest of the elements, and when I sort for a column name, order for that column but show first the elements with "valueC" and later the rest of the elements sorted by the column that I clicked.
To do that, I can sort the elements in the ObservableCollection of the view model, but I think that this functionality is more something that it would be done in view, plus because I would like to sort by columns, so I would have to say to the view the column that is clicked.
However, I don't know if it is posible to do this kind of complex sorts in the view, perhaps with some converter or with any way.
Thank so much.
Every logic has to be done in the ViewModel. View must not know anything about the logic behind displayed datas.
In a simple case you could order your items in a view but in a realistic case, ordering will not affect only visible items but it affects also items not shown because of the paging.
Related
i use WPF and add WrapPanel children in User Control panel.
like that:
SearchPanel sp = new SearchPanel();
sp.Clock = clock;
sp.Name = item.CustomerTwo;
sp.Color = "Purple";
wPanel.Children.Add(sp);
how i organize this added panels
The simple answer to this question is that they appear in the order that they are added to the Children collection.
If you were adding them explicitly using XAML they would appear in the same order that they appear in the XAML.
If you were adding them by binding the ItemsSource of the WrapPanel then they would appear in the order that the bound collection presents them.
As for your specific example...
In your linked code, the colour is based on a date indirectly obtained from each "customer" item. To order the WrapPanel based on this, you will need to sort your customer items by that same criteria first.
Unfortunately, the date logic is convoluted (using a DB relation to a Days table and then some awkward date manipulation code). You could use that convoluted date logic in a Comparison function to sort the customer items first. Or you could add a property to the "newSearchPanel" class which you calculate while constructing them and add them to a temp list, which you sort before finally pushing them into the WrapPanel.
I have about a dozen ObservableCollections that hold objects which have dates, singles and integers. All collections are of the same length and have data added and removed at the same time. One of the ObservableCollections is the main one and is needed when referring to the others.
Is there a way to use CollectionViews to bring 2 of these ObservableCollections together in such a way that they look like one ObservableCollection having the total of all of the original columns when data bound to a datagrid and/or chart? If so does anyone have an example?
Everything I have found shows the data from both sources being brought together by adding one on top of the other in what I would call a stack of data .
Thanks
Not pretty but I have a similar application.
Have a class with 5 base properties then a variable number of fields.
The fields I put in a List.
Can bind rows to a collection but not columns.
So I use a ListView Gridview where I build up the columns in code behind.
Bind the columns to Field[0], Field[1], ...
In you case you could have base class Bclass with the base Properties and a Property List
In your List<Fields> you just iterate the properties of the List<ExtensionClass>
Clearly the List<Bclass> needs to all have List<Extension> class with the same length in every item of the binding breaks.
How can we get Number of rows Present in Listview?
Couldn't you just use ListView.Items.Count?
Well, you see. When you bind a list view to some sort of collection (preferably some sort of observable collection) with some sort of type.
You can add/remove.
But you also have the functionality to say
observablecollection.count.
Now if you are adding the items inside the XAML.
a quick and easy way to access it is.
x:Name="name of the list"
then in the code behind say
(name of the list).count.
That should give you access within the code behind, or if you don't want to do it in the code behind. You will need some sort of window dictionary.
and you can access the (name of the list) through your dictionary.
I am working on an MVVM-based WPF app that uses the DataGrid. The grid is data-bound to an ObservableCollection<Widget> (the Widget list). The user needs to be able to insert a Widget into the list below the currently-selected Widget, to delete a selected Widget, and to reorder the Widgets on the grid. I'll implement the reordering through dragging and dropping.
Before I dig into this, I want to get a reality check on my approach. Here is what I am planning to do: The Widget class will have an Index property that will be used to order the Widget list. Additions, deletions, and reordering will be done by manipulating this Index property, and sorting will be done on the Widget list, rather than through a CollectionView.
Before the DataContext is set, the Widget list will be sorted on the Index property. When a user adds a Widget, the Index property will be incremented by 1 for each Widget below the selected item on the grid, and the new Widget will be given the index number opened up by the renumbering. The Widget list will resorted, and the bindings will be refreshed.
When a user deletes a Widget, the Widget will be removed from the list, and the Index property of items on the grid below the deleted item will be decremented by 1. The Widget list will be resorted, and the bindings refreshed, as above. Reordering will be done as a combination delete-and-insert.
Here is my question: Is this a reasonable strategy for implementing inserting, deleting, and reordering? Is there a better way to go about it? Any good articles or blog posts on the subject? I've looked, but I'm not coming up with anything on-point.
Thanks for your help.
I've got this problem figured out. I do need the Index property, but I don't need to sort the ObservableCollection. I create an interface, IIndexedObject, with one property, Index. I implement that interface on any object that needs to be indexed, and in my database table, I add an Index column.
When I load into my object model, I sort the database table on the Index property. That ensures that the list is loaded in the same order as in the last run. I add objects using the blank row provided at the bottom of the DataGrid. It gives them an index of 0. I delete objects using a Delete button bound to an ICommand in the view model.
My view model subscribes to the CollectionChanged property of any ObservableCollection that contains IIndexedObjects. When the event fires, the view model passes the collection to a ReIndexCollection service method, which takes an IIndexedObject. The method re-indexes the collection, based on its current order, by simply iterating the collection, assigning an incremental integer value to each element's Index property. When I persist the collection back to the database, the Index value gets saved, to ensure that the collection is loaded in the same order on the next run.
Since the only sorting that is needed is done at the database load, there is no need to sort the ObservableCollection. When I move items on the DataGrid, it will take care of re-ordering the collection at that time. All I have to do is re-index the collection, so that its order will be persisted back to the database.
All-in-all, it's much easier than I had expected. One of the reasons I like WPF and MVVM.
Improving sorting performance:
http://blogs.msdn.com/jgoldb/archive/2008/10/30/improving-microsoft-datagrid-sorting-performance-part-3.aspx
I have a list of string array as input. The array dimension are static for all the list but I can't know the array size until the list is retrieved.
I need to bind the list in a datagrid in Silverlight.
I tried to create columns at runtime, binding each column to a particular array position but cannot find a way.
Have you any idea how to do it?
I could eventually "convert" my list of array in anything else that could be binded in a datagrid?
The idea is to have a sort of 2d Matrix showed inside the datagrid in Silverlight (I think that the problem is similar).
List
column_1 column_2 column_3 .. column_m
string[1,1] string[1,2] string[1,3] .. string[1,m]
string[2,1] string[2,2] string[2,3] .. string[2,m]
string[3,1] string[3,2] string[3,3] .. string[3,m]
....
string[n,1] string[n,2] string[n,3] .. string[n,m]
n is list lenght, m is list column max number.
Any idea is appreciated
I've found two solution to the problem that use the schema in the Denis's answer:
the first one is to use reflection to generate a class at runtime for the binding as suggested in this article (thanks to Vladimir Bodurov). I've tested this solution and I'll try to use it on my project. The only problem right now is that for large collection, the performance are poor. But I hope that someone will fix it in next relese (Silverlight 3 seems to not have fixed this problem, yet)
the second solution will be using some dynamic language for generate data. I don't know if this could be faster or not (probably not) but eventually could help. I will try in the future and use ironpython or ruby to generate classes that will be binded in datagrid.
If anyone have tryed using the second solution or any performance related information about creating classes at runtime, it will be appreciated.
Giorgio
Unfortunately that's not gonna be easy.
Do you have any valid constraints, like the maximum number of columns that is allowed or anything like that?
If you do (let's say you have N column maximum), you might be able to do something by
Having a class that exposes N
properties (named Col1...ColN for
example) that map the content
of the array for one line at column
X
Generating a list of that class,
one instance for each line
Generating the correct number of
column on the fly, binding each
column to property ColX
Binding your DataGrid to that list
That's kind of ugly, but it would work.
If you do not have to rely on the DataGrid, there is a possibility using a UniformGrid. A UniformGrid is a panel that layouts its children in a regular grid (every item has the same width, every item has the same height). You can indicate the number of columns at runtime, and the panel will fit children one after the other up to the number of columns and then continue on another line.
You could bind an ItemsControl to your array, indicating it should use a UniformGrid as its layout panel and indicating a suitable ItemTemplate to render each string.
The second option is much easier, but you will not have the capabilities of the DataGrid like sorting, moving columns, row selection, edition events per row etc.