Linq to SQL Compact - Update binding - wpf

When I set the ItemsSource of a ListBox to the contents of a table, like this:
this.listBox.ItemsSource = db.Table;
The items are not updated automatically in the ListBox. How can I manage to update the ListBox automatically when items are added, removed or changed? And can I also receive an event when the collection has changed?

Take a look at ObservableCollection. I'm using it to update/add/delete a listview. When I change the ObservableCollection, the listview gets notified.

Related

Bind dynamic value coming from GUI to the viewmodel

There is an itemscontrol in one of my views that lets the user reorder the items by an up/down button.
My question is on how I can flow the current index of the items to my viewmodel
The following questions address on how to find the current index (not so clean but it does the job)
WPF - Bind to Item Index from within ItemTemplate of ItemsControl?
Now, how can I modify the XAML so that it binds this index to a property on my viewmodel?
edit The question is about how to do it in XAML declaratively. A possible solution is to do it in code using and ObservableCollection and subscribe to CollectionChanged
Kind Regards, Tom
So your items store an index poperty that you need to update when they are re-ordered in the UI?
I would create a view model that exposes my items as an ObservableCollection. You can then handle the CollectionChanged event which will fire when the items are re-ordered in your view model. At this point you can enumerate the collection of items updating their indices.

How can you update a WPF Datagrid where ItemSource is a Linq-SQL collection that has .Refresh() executed on it?

I have a datagrid which has its ItemsSource bound to the result of a linq query (.ToList())
I can make changes to properties of the collection bound to the itemssource, and these changes are reflected immediately in the datagrid fields, such as;
myQueryList[2].myProperty = newValue
What I can't do though, is see the changes made in the database reflected in the datagrid by this;
myQueryList.Refresh(RefreshMode.OverwriteCurrentValues, myQueryList[2])
I have inspected the value of myQueryList[2].myProperty after this refresh, and it shows it has correctly updated from the database. Why does the datagrid not display it, and how can i get the datagrid to display it?
Also; I have found the same problem with using an ObservableCollection
Thanks to Casey's (edited) response in this post, i've found a workaround
I've implemented the SendPropertiesChanged() on the collection members and call it whenever i do a Refresh()
Once you convert a query (deffered/dynamic) to a collection (fixed) using ToList() method, a separate collection is created. This new collection has no connection to the query. When something changes in query, it does not reflect on collection. You will need to reset the ItemsSource property whenever you call Refresh() on query.

How to check when silverlight Listbox is updated

How do I check when a listbox is updated, or when its item count changes? I can only capture the SelectionChanged event now.
I'm trying to select any new item when initially added to the listbox binded DocItemCollection.
If your DocItemCollection is an observable collection (or at least implements INotifyCollectionChanged) you can watch it by subscribing to : DocItemCollection.CollectionChanged and see if it is an insert or a delete.

Master View implementation using MVVM in Silverlight

I have a silverlight datagrid to which I am binding an observable collection from the viewmodel. There is a detail view page which will display different properties of the object in the collection when user selects a row of the datagrid. My requirement is when user updates any properties in the detail view; the data should be updated in the data grid also. How to implement this functionality?
Well, the answer is simply to bind both the datagrid row and the control displaying the selected object. The simplest way is to use a ICollectionView (returned by a CollectionViewSource from the original ObservableCollection), bind the grid's ItemsSource to that, and then bind the control's DataContext to the ICollectionView's CurrentItem. That way, when the grid's selected item changes, the CurrentItem of the ICollectionView is updated, and that item is displayed in the detail view.
I think it's quite easy but if you need additional details or sample source code I'll elaborate.

Silverlight listbox won't update with new itemsource

I have a listbox which is bound to a generic list, whenever I removed an item from the generic list and rebind it to the listbox it still shows the deleted items. Here is the code :
InventoryList.Remove(currInv);
lstSubMenu.ItemsSource = InventoryList;
lstSubMenu.DisplayMemberPath = "InventoryItemName";
I checked the generic list and the item is being removed and there doesn't seem to be any errors in the output window.
Set the ItemsSource = null before you set it to be InventoryList.
However, it is generally better practice to set the ItemsSource property once and never again. You can do this by using an ObservableCollection. Once you do this you can add/remove to your heart's content and not have to worry about the binding target not getting updated.

Resources