Silverlight listbox won't update with new itemsource - wpf

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.

Related

WPF TreeView ItemsSource not keeping values

I am using a wpf treeview and binding the ItemsSource to an IEnumerable of my ViewModel that has an IsChecked Property. I am binding a checkbox to this value with a Mode of TwoWay. I can see when I step through the program that it is setting this value properly on my ViewModel when I check the checkbox.
I then have a Menu Item that "Runs Checked". In this method I have a foreach loop that runs through the ItemsSource as IEnumerable of ViewModel looking for IsChecked = true to queue up the checked items to be run by a separate program. As such:
foreach (AccountViewModel account in tvClientList.ItemsSource as IEnumerable<AccountViewModel>)
{
if (account.IsChecked)
{
context.Queues.InsertOnSubmit(new Queue {Id = account.Id});
}
}
However, account.IsChecked is always false. Why is this?
i would check the following.
does the setter of AccountViewModel.Ischecked is called when clicking the treeview checkbox. if no - then thats the problem if yes you should look at your collection bindings.
debug your foreach and check wether the treeviewitemssource is the collection you expect.
maybe you can post your bindings, if that all no help.
ps: dont access your View controls directly if you use viewmodels/mvvm.
Just for anyone else encountering this issue. ItemsSource returns a new set of data, Items, however contains the current set of data.

Clearing selecteditem of listbox (which is bound to collection of objects) with MVVM

I have a listbox with silverlight 4. I have the list bound to a list of objects.
1.) The SelectedValue property is bound to a public property of the viewmodel called Current. How do i clear the selection? I have tried setting the value of Current to null. Well, this clears the selection however it also breaks binding in the edit form which has a combobox bound to a property of 'Current'. Textboxes which are bound to Current.FirstName etc. are working ok however the comboboxes do not function after I set the Current object to null.
2.) how do i load the form without the first item being selected?
Found a work around for this bug in Silverlight:
// Bug in SL listbox prevents SelectedIndex = -1 from unselected.
// Workaround is to use DispatcherBeginInvoke to do it async. Found
// work around here:
// http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx
Dispatcher.BeginInvoke(() => { QuickItemsListBox.SelectedIndex = -1; });
More details here:
http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx
Not sure when it was fixed but VoodooChild's answer works now in Silverlight 5. Passing this along in case others are looking.
yourCB.selectedIndex = -1;
Try:
yourCB.SelectedIndex = -1;

How can I add items after setting itemsource to ListView

I got exception :
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
You must add the items to your source-collection that is set to ItemsSource. After you have set the ItemsSource-property to a value, its no more possible to use the Items-property (besides you set ItemsSource newly to null). Items and ItemsSource exclude each other.
If you use ItemsSource, your source-collection must implement INotifyCollectionChanged so that it will inform the ListView about changes. If not, added items to it will not change the ListView. E.g. if you use List<T> as items-source, changes will not be forwarded.
There exist classes that do that for you such as ObservableCollection<T>.

Silverlight: Listbox bind to CollectionViewSource\List update on remove of item

I have a Listbox that I binds to a resource (sort) CollectionViewSource in my XAML. Then in my cs code I set the CollectionViewSource source to List of objects (class level field)
I then have "remove button" that checks the selected items in the Listbox and removes them from the List of objects (class level field).
I thought the Listbox should update automatically since the items source updated.
Am I missing a step or property setting ?
Or am I missing something about how binding works?
tep
The class that contains your list of objects must implement INotifyPropertyChanged and you must raise the notification event when the list changes, passing in the name of the property that changed. This is what notifies the UI that it must update anything bound to that property.
Alternatively, make your collection of objects an ObservableCollection<T> and that will do the notifying for you.

Linq to SQL Compact - Update binding

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.

Resources