I am fetching data from an URI and parseing the xml to populate my ObservableCollection<"classname"> and showing it on the GridView in my WPF project. The problem I am facing is, when i delete an entry from the ObservableCollection, event is triggered and GridView is updated. However if an entry is updated on the server from which i am getting the data from, no event is triggered on the ObservableCollection and list is not updated.
I have tried reloading the complete object list again on click event but still no changes can be viewed in the GridView. Any idea how to do this?
ObservableCollection only reports adding, removing or replacing items in it. It can't know anything about changes in the internal structure of the stored objects.
If you want changed in your objects reflected in the GUI, you should implement INotifyPropertyChanged.
Alternative approach would be to remove and then add back the item that changed, but it's not as clean as the previous solution.
Related
I am working on a reporting app using PivotViewer. There are two controls on my grid. One is PivotViewer and the other one is a reporting Panel. After a user changes the filter of the PIvotViewer, I will generate the report on the report Panel in real time based on the remaining items in the current collection of the PivotViewer (InScopeItems). FilterChanged seems like the perfect event to hook up. However, it seems FilterChanged event is fired BEFORE the filter change.The InScopeItems don't change in the call back.
What I want is an event AFTER the filter change.
Right now the reports are very funny because it's showing the last report before I change the filter.
What's the recommended event? This seems like a very common user case but I couldn't find any solution. Thanks!
The best way to track changes to InScopeItems is to track the property itself. If you case is to a INotifyCollectionChanged object, you will have access to a CollectionChanged event. This should get you where you need to go.
Here is an example:
(pViewer.InScopeItems as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(MainPage_CollectionChanged);
I have a WPF application based on the MVVM pattern. It has a tree on the left side and a details area on the right side. The tree contains objects of various types and I have a view model for each type. In the view, I have a different data template (containing a user control) for each view model type. The view is then selected via databinding based on the current details view.
Now, when I switch between tree nodes, I also instantiate a new details view model for each node and the view gets changed accordingly, firing both the DataContextChanged event and the Loaded event. That is, until I start to switch between objects of the same type. Here, too, the details view model gets updated, but the view instance stays the same. This means, only the DataContextChanged event is fired, but not the Loaded event.
Is there a way to force the Loaded event to fire, for instance, by re-initializing the view?
The reason why I need the Loaded event is that WPF fires the selection change events on input controls during data binding and since I have logic that acts upon user-triggered selection change events, I need to be able to ignore those triggered by data binding. For now, the Loaded event seems to be the best option to do this. Escept for the described issue.
Thanks, Michael
Move whatever logic you have in the Loaded handler to the DataContextChanged handler if it needs to run every time the data context changes.
I've got a Silverlight application that has created a master/detail view of data coming from an OData feed (my first app). When I make changes to an item in the detail view, they are showing up just fine in the master, but the user has no indication on what has changed (including adds & deletes).
For some reason I can't find a property that indicates that the collection has changed. There is a CollectionChanged event, but that's only for items it seems, not for their properties.
Is it possible to easily determine what items have changed in a DataServiceCollection and easily show which ones those are? I expect I could build something that walked through the collection, but that's not idea.
There is INotifyPropertyChanged event, that gets fired when the properties are changed. Does that help?
Thanks
Pratik
I appologize for the novel but I wanted to explain as much as I have done thus far.
Within my current project I have an application that consumes a service that provides a collection as a <List>. Due to how I am using this data in the application I have had to convert this data to an observable collection. This was done so that as the data was selected and moved about the application UI updates would be refreshed using INotifyPropertyChanged and INotifyCollectionChanged.
Where I am having a challenge now is I have a listbox that is bound to the observable collection within the listbox I have a datatemplate that renders out the items of the collection. This data template contains a button which needs to allow the user to click the button for each item to remove them from the collection.
The use case for this is a listbox that stores selected name as chosen from a gridview. Once the user has selected names from the gridview they are stored ( within the observable collection as a queue) and rendered out in the UI in a listbox control which shows all selected names. I need to provide the user with the ability to remove these names in any order selected.
From what I have been reading there is no means to enumerate / index an observable collection. For situations such as this you should use List or an Array. However in order for the items to refresh in the list view they need to be in an Observable Collection.
From what I have read it appears that when the event is triggered I need to convert the observable collection to an Array and then evaluate the array to determine the index and then remove the record accordingly?
I think I may be off base on this as it seems like I am over engineering this problem? The above scenario does not seem correct is because I fell as if I am doing a lot of converting to and from the collections to just remove a record?
Does anyone know of an efficient means to remove records from a collection ( in any order selected) when the collection is rendered out as an items control within a listbox?
I’ve been successful in removing the last record added to the collection using RemoveAt() however I have not had any success in randomly removing records.
Afterthought: Part of this issue could be related to the fact that I have a button inserted within the datatemplate (control item) and as a result the item is not actually being selected before the event is fired on the button event?
Sorry for the rambling on this but I have had my head in this for hours and made minor progress. Any tips or ideas would be appreciated!
ObservableCollection<T> inherits from Collection<T> which implements IList<T>, so you can certainly index and enumerate it. It has a Remove method that takes the object to remove and removes the first occurrence in the collection and a RemoveAt method that takes an index and removes the item at that index.
Based on your afterthought, it sounds like you have a WPF ListBox with an ItemTemplate that creates a Button. ListBox will set the DataContext of each instantiated template to the item in the list being bound to, so you can get a reference to the item that created a Button from the DataContext property on the Button or by using a Binding.
I can't believe I haven't run into this issue before: I have a data bound form and before the user navigates to another record I want to save some additional data from non-bound controls in addition to the bound data. The only event I can find is BindingManagerBase.PositionChanged but this seems to happen after the current record has changed.
Is there a way to get an event when the data has just been saved but before the binding has moved to the new record?
Have you looked at implementing a ListChangedEventHandler to detect when your list changes? The list seems to raise an event of type Reset when the list position changes (assuming your data is in a BindingList<>).