ListView + POCO - c#

Can I somehow use POCOs in a ListView? Or are ListViewItem's my only option?
If I can only use ListViewItems is it possible to assign a value object (my POCO) or is it possible to use subitems with value objects?
Thanks in advance!

You can use ObjectListView which plays very nicely with POCOs.
Update
ObjectListView is based on the regular ListView, but it uses some magic and the win32 api to make everything possible. It's not very hard to get started with it and I don't really see why you can't use it?
Standard listview: All options (except owner drawing) include using a ListViewItem. If you got a large collection I recommend that you use Virtual Mode which means that the list view will request each item that is visible.

I just found out the ListViewItem.ListViewSubItem class Tag and Text work fine for me. No Pocos though, but I can keep the original values in their original data types.

Related

XamDataChart with unknown number of series

I am using an Infragistics XamDataChart and want to bind a collection in my view model to the chart's Series property, since I don't know in advance how many line charts I will need to display.
From what I can gather from old posts in the Infragistics support forums, the Series property is read only and thus doesn't support binding directly. A solution is offered here but it seems like overkill for such a simple goal (maybe to me it just seems simple).
Has anyone here done any work with the Infragistics xamDataChart and MVVM? The ultimate goal is to be able to have a collection in my view model that contains a variable number of 'series' that I can just bind to the chart. Now I can probably do this if I just write some code behind for my xaml, access the DataContext (viewModel) and listen to the collection property, directly adding/removing series to the chart as necessary, but I was looking for a more MVVM way.
Thanks.
Since the Series collection of the XamDataChart is read-only, in order to be able to generate the Series dynamically,based on you VeiwModel, you should use helper class, similar to the approach that Graham Murray has suggested in the thead that you have referred. I have created a sample applicaiton, that show how you can create similar appraoch for binding the series of the XamDataChart to collection of your ViewModel. You can download the sample from here:
http://users.infragistics.com/Samples/SeriesBinder.zip
Sincerely,
Krasimir

How to dynamically update a listviewitem through code

I have a listview and would like to update the text of one of the columns for a specific listviewitem (row).
How would I go about doing this?
Hard to say without any context because there are so many ways you could populate your list!
The generic answer is you bind your list to a collection view which itself binds its source to your viewmodel (or you bind directly to your viewmodel if you don't need CollectionView features).
When you want to modify your list, you make sure you raise a modification notification on your property, and XAML binding will take care of updating everything.
It is really basic stuff on dependency property and binding, you should read more about this topic. MVVM-light is a very light framework that allows you to take care of all kinds of binding-related issues with a very clean and neat flavor. You will also find some very good self-explanatory webcasts from the author of the website about all those topics.

Set a hidden property to a ListBox Item - C#

hey,
Does anybody knows a way to set a hidden value to a ListBox Item.
As an alternative i can use another listbox simultaneously.
Thanks.
You are probably looking for the Tag property. It holds an object type, which means you can save a reference to anything in there.
Although before setting on using the tag property, take a look at ItemsSource. Basically you can tie a collection of things to be the provider for your listbox, it's usually a superior alternative to dealing with Listbox items individually.

How do I access the controls in a WPF DataGrid

In good old (well!!) WinForms days the datagrids row used to the be the actual control and you could then access the DataItem.
In WPF its all flipped and dataGrid.Items is just the source data.
I am probably doing this the wrong way round as im a bit of a WPF newb but how can I iterate through the rows of my gridview grabbing the values from certain labels, textboxes etc?
Yes, you are doing this the wrong way round. What you should be doing is iterating through the items in your data source - it's where all the values are, after all.
It's possible to iterate through the WPF objects, but it's not trivial. And there's a significant problem that you'll run into if you try.
You can use the VisualTreeHelper class to search the visual tree and find the DataGrid's descendant objects. If you play with this long enough, eventually you'll figure out how to find the specific controls you're looking for. But the DataGrid (actually, the VirtualizingStackPanel in its control template) virtualizes its visual children. If an item hasn't appeared on the screen yet, its WPF objects haven't been created yet, and you won't find them in the visual tree. You may not be able to find what you're looking for, not because you don't have a way of finding it, but because it doesn't exist.
If you're using value converters and formatting in your bindings (which is the only reason I can think of that you'd want to look at the WPF objects and not the underlying data items), I'm afraid the answer is: don't do that. Do the value conversion and formatting in your data source, and expose the results as properties that can be bound to directly.
It's certainly possible to use WPF without using the MVVM pattern. But this is the kind of brick wall that you can run into if you don't.
You can use this
public DataGridRow TryFindRow(object item, DataGrid grid)
{
// Does not de-virtualize cells
DataGridRow row = (DataGridRow)(grid as ItemsControl).ItemContainerGenerator.ContainerFromItem(item);
return row;
}
where item represent the data displayed on the row.
Hope this helps.

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.

Categories

Resources