Silverlight 3 - Filtering an Observable Collection - c#

Is there a way to filter/sort an observable collection and still keep the notifications?
I have looked around and I found CollectionViewSource which filters and sorts the collection as I require but when the an items property in which the filter relies changes at the source collection it does not refresh the filter.
Basically i require a view of the original collection that when a property of an item in the view changes it updates the source and when the source changes it updates the view. Is there any class that provides this functionality in silverlight 3?

Does ObservableCollection with TwoWay binding not work? Can you elaborate your example with some code to show the issue in more detail?

I would suggest using the Bindable.Linq library, it hasn't been updated for a while and there is a bug with the Union operator. But for linq style filters it works great.
heres a quick example, assuming this is in the codebehind of a silverlight user control with a listbox named people:
using Bindable.Linq;
...
ObservableCollection<Person> data = new ObserableCollection<Person>{.... fill in};
people.ItemsSource = data.AsBindable(Dispatcher).Where(p => p.FirstName.Equals("steve"));
data.add(new Person("steve"));
if you do this steve should show up on the list. I have found this library very useful and if you download the sample projects from codeplex it shows more advanced examples.
Hope this helps.
There are several other similar projects

Related

Load ObservableCollection from Xml save file to Datagrid (C#, Wpf)

Okay so I hope it's okay if I don't post my code since it contains private parts.
I will describe it as good as I can. So I did an observablecollection without onpropetychange and a databind to the grid. Everything works well if I'm adding something to the collection, the datagrid updates. But if I get the observablecollection from the xml save file, it doesn't update.
So far I checked if the observablecollection loads everything (it does) and tried to update the datagrid manual (nothing). I'm glad if someone could give me advice without seeing the code. :)
If you are changing the whole collection, the UI can not recognize the change since you are not using the INotifyPropertyChanged. You have two options:
Implement the INotifyPropertyChanged and raise the event after setting a new collection as source.
Clear the old collection, and fill in the new items to the old collection.
My best guess is that you should set the DataGrid's ItemsSource property again after loading the ObservableCollection.
Use this:
datagrid.ItemsSource = null;
datagrid.ItemsSource = yourObservableCollection;

Creating binding source vs just assigning a List<C> for combobox?

In the following link, the author shows how to create a binding source and add sorting function to a grid view. It should work similarly for combobox datasource. However does it have any benefit to do it for simpler controls like combobox or listbox? I can just assign a list of type List<AClass> and then assign the DisplayMember and Value for the controls.
http://aviadezra.blogspot.com/2008/06/binding-domain-objects-in-visual-studio.html
Even for data grid view, does it work well for complex situation like, for example, sorting on a grid view with paging? Looks the class PropertyComparerCollection in the example only works the loaded data.
The article is focused on binding complex business objects to the grid view. This has been a intensively debated subject since .Net Framework 2.0. If the properties of your bound type are no longer primitive, then you have to get your hands dirty to support sorting, filtering or searching.
There are plenty generic classes for this goal, but I would recommend this implementation, with a nice video demo here.
However, if you're not aiming at a data grid view, but rather at a combo box or list box, and wish not to modify the underlying store while sorting or filtering, you could still use a custom class derived from IBindingListView and BindingList(of T), like the one suggested above and manually call ApplySort or ApplyFilter. The implementation in the article linked in your post and the one I suggested use temporary copies of the original data source for sorting/filtering.
Furthermore, for paging instead of taking slices out of the original data source, you'd be taking slices out of the wrapped sortable view of that data source.

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.

Sortable Object Collection

I'm look for some direction.
I need a Sortable collection of objects which can also notify when items are added / removed from this collection as I'm binding it to a menu items as a list of windows open in my application.
Could someone please advise which would be a good collection type i.e. List<>, ObservableCollection et cetera and how I would go about sorting the said collection.
Many Thanks in advance.
This is sortable observable collection
And here is another implementation - I use this one in my project, works flawlessly (I just had to extend it so it implements also IList interface, so it can be used to define collections in XAML).
You could try and wrap your ObservableCollection in a CollectionView, notifications will be propagated and you can sort, filter and group items.
Note that the sorting does not modify the source collection which might be a problem if you need the changes to be permanent, then again only yesterday i used the class for the first time so don't know much about it, maybe you can apply the sorting to the source somehow.
Have you tried using System.Windows.Forms.BindingSource along with a System.Windows.Forms.BindingNavigator? These will do exactly what it sounds like you need- bind a collection of objects to a navigable menu.
As for sorting, you just have to get a list of the BindingSource's items, clear the BindingSource, do your sorting on the list, and add the sorted items to the BindingSource again.
See these links for helpful examples:
http://msdn.microsoft.com/en-us/library/b9y7cz6d.aspx
http://www.code-magazine.com/article.aspx?quickid=0507051&page=2
http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2008-09/msg01820.html
http://blogs.msdn.com/b/dchandnani/archive/2005/03/09/391308.aspx
Thanks for all the responses.
Was able to simplify things as I realised I only needed the collection to be sorted for display purposes when binding to the menu items.
I was therefore able to use the following code to sort my list of panel objects via menu item parent (miPanels) in this case. (PanelName being one of the properties of the object)
miPanels.Items.SortDescriptions.Add(new SortDescription("PanelName", ListSortDirection.Ascending));
Once again thanks for all the people who took the time to look and respond.

Categories

Resources