WPF LISTVIEW Count The Rows - wpf

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.

Related

Order elements in view or in view model?

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.

Hide Elements in WPF TreeView

i have the following scenario:
I have a ViewModel with hierachical elements to display in a TreeView. So far so good. What i want to do now is hide/remove elements from the TreeView according to some property set on a ViewModel-Element, like IsConfigurable or such.
If i disable die DataTemplate, the element is removed, but also all child-elements, which is not what i want.
Is that even possible?
Greets,
Jürgen
That sounds somewhat strange, but nevertheless...
You should consider that your application shall remain test and debuggable.
Your model contains the orginal data (collection) as it is - no meddling here. In your ViewModel, the object that you are binding to, you can calculate the transformation as you want to display your hierarchy. This approach has the benefit, that you can "easily" test/debug your transformation. Now bind your TreeView to the calculated hierarchy without obscure experiments. If properties in your ViewModel (you mentioned IsConfigurable or whatever) change, you know when to re-calc your bound hierarchy.

Retrieving Cells within a WPF DataGrid Column

I need to get all the cells within a certain column.
there is no property called "Cells" in the GridViewColumn class.
Is there any other way of doing this?
Thanks.
May I ask you the purpose of getting those cells? And when you say you want a cell, do you want the visual of that cell or the data associated with it?
If it is data, then getting it directly from your ViewModel will be a preferred approach. So if you are following proper MVVM while implementing this, you can have a method in the ViewModel which can easily expose you a specific collection of cell-data corresponds to a column
Have a look at http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

WPF combo box, adding an all options item, when binding to an Observable Collection

So I've got an object that has an observable collection. Most places I bind to this collection I only want the user to select a single item. But in one place I want the combobox to include an all items option.
Is the way to do this simply with xaml converter?
It seems like doing it in the view model would be a good idea, but it's really a lot dependency object goop to basically wire up an collection that is two objects deep for an on change event, where as the xaml converter just works.
But I always feel like xaml converters should be generic and reusable, where in this instance, the xaml converter would be more of a one off.
Of course the third option would be to create one off list for UI in the object that contains the standard observable collection. But this seems to be mixing the logic and presentation in a way that makes me uncomfortable.
Take a look at CompositeCollection. It lets you concatenate multiple collections into a single collection -- sort of the XAML equivalent of the LINQ Concat. It implements INotifyCollectionChanged, so it should be able to react to changes if some of its inner collections are ObservableCollections.
CompositeCollection also lets you add individual elements, so you wouldn't even need to wrap your "All Items" in a collection of its own. (Instead, you have to wrap collections in a CollectionContainer. See the examples on the CompositeCollection doc page.)
I had a similar need, and ended up using the solution discussed in this article: How To Select Null/None in a ComboBox/ListBox/ListView.
You can replace the "(None)" string on line 98 with "All" or something similar. Then just add logic in your ViewModel to treat a null value as "All" in whatever manner is appropriate. It's a little annoying to have an extra class lying around but it makes the code a lot cleaner if you do need this in several places.

Array binding Xaml by Position on Datagrid

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.

Resources