How to check when silverlight Listbox is updated - silverlight

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.

Related

WPF telerik GridView aggregate is not updating until focus out of a grid view field

telerik:GridView, using aggregate function to show sum in footer, the property is being updated from somewhere else and GridView is not calling PropertyChanged until focus out from GridView Column.
How we can refresh aggregate as soon as Item-source Property is changed.
I want to get aggregate updated as soon as ItemSource Property is updated.
On GridView property change I am using
private void GV_PropertyChanged(object s, PropertyChangedEventArgs e)
{
GV.CalculateAggregate();
}
According to the docs, it seems like you should raise the CollectionChanged event for the source collection:
In order to get the aggregate results refreshed, you have to notify
the GridView that the bound collection has been changed. A
CollectionChanged notification of the bound collection should be
raised.
There are four possible solutions:
You can update the value as illustrated in this help article on how to
edit an item outside RadGridView.
Ensure that a CollectionChanged event is raised from the bound source
collection.
Invoke RadGridView.CalculateAggregates() method.
Invoke RadGridView.Rebind() method. It will raise CollectionChanged
notification with action Reset. Please note that the entire view will
be recreated.

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.

ObservableCollection DataGrid

I bound the ObservableCollection to the dataGrid itemssource.
the collectionChangedEvent of the observable Collection is getting called only when we add, delete, remove. But not firing when we update the record.
how to fire the event for Update too?
If you want to be notified when an item is changed (ie you want to subscribe to this event), you are out of luck with ObservableCollection<T> because this collection only fires the CollectionChangedEvent.
Indeed, if you implement INotifyPropertyChanged, you will see changes to the items in the view (WPF does this automatically), but if you need to execute manual actions when an item changes, you can use BindingList<T>.
For exactly this scenario I rolled out a custom BindableCollection<T>, which implements ObservableCollection<T> and adds the OnItemChangedEvent. I can provide some sample code if necessary...
The collection doesn't know when the record is modified. To get a notification when this happens, the record needs to implement INotifyPropertyChanged

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.

How do I BringToView the latest item added to a bound ObservableCollection

I have a silverlight control (View) which displays a list of items in a specified property of the datacontext (viewmodel).
What I need is for the scrollviewer in my control to scroll to the top or bottom depending on where the latest item has been added to the list. (It'll always be either the beginning or the end of the list, I don't need to worry about middle of list insertions.)
In WPF i'd just use the DataContextChanged event to start listening to the viewmodel, but in silverlight that event is internal.
Any ideas on how to tackle this?
A good starting point is Attached Behaviors on CodeProject.
A useful behavior would watch the ListBox.ItemsSource and attach to the observable collection when set. On the collection changed event, use ListBox.ScrollIntoView to display the changed item.
I can't use the CollectionChangedEvent of ObservableCollection since I need the DataContextChanged event to get the DataContext which holds the Collection in the first place.
Would you not do this in the ViewModel?
Whatever ViewModel has the ObservableCollection, expose a property of type T named SelectedItem and whenever the ObservableCollection changes with a new item, the CollectionChanged event will allow you to set the SelectedItem property. Once this is done, wire up the SelectedItem in the control to this property on your ViewModel.
This will obviously only work with controls like ListBox where a SelectedItem property exists.
In place of DataContextChanged in WPF , you can use CollectionChanged event of ObservableCollection. In the collection changed you will get to know the NewItem Index.

Resources