I have a wpf application and im using winform host.
In some cases the 'SelectedItem' property is complex type that have a property from type 'List'.
How can i know from code if the collection changed? Or just know of the custom editor opened?
You can use a ObservableCollection<T> instead of a List to have the functionality of indicating changes to the contents of the list.
The ObservableCollection implements the interfaces INotifyCollectionChanged and
INotifyPropertyChanged to archive this behaviour.
More information to ObservableCollection on MSDN:
https://msdn.microsoft.com/de-de/library/ms668604(v=vs.110).aspx
If I understand well you have collection of elements, and each of them have some other collection.
In that way you could also consider using BindingList as your base collection - it implementsIRaiseItemChangedEvents - Indicates whether a class converts property change events to System.ComponentModel.IBindingList.ListChanged
Related
I used the List<Person> collection as the ItemsSource for the DataGrid control.
But it did not update the View if i remove the item from the List collection. I was struggling for a long time for the solution.
Then Instead of the List<Person> collection in my ViewModel. I changed this into ObservableCollection<Person> collection. Now it updates the view when ever there is a change in the collection.
I am not sure why it updates only for ObservableCollection<Person> ? Anyone ?
Well its in the name. A simple List doesn't tell the ui to update, in other words "the view can't observe the list". There is no weird magic behind Databinding. WPF and DataBinding needs the Data model to tell them "this is new" or "this is changed", you propably already saw INotifyPropertyChanged, INotifyCollectionChanged is the same but for collections, and the List<> doesn't implement it, ObservableCollection does.
Because List does not implement INotifyCollectionChanged
Because the update of a databinding is not a kind of magic, there are several requirements to make databinding working correctly. If you have a single property to bind on this property must be either a dependency property or its parent class must implement the INotifyPropertyChanged interface to notify the wpf binding system about changes of the property value.
For a collection there is a simelar mechanism: it must implement INotifyPropertyChanged to inform the wpf binding system about removed/moved/added items.
See here for more details: http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx
ObservableCollection<T> fires change events every time you change the items in the collection. List<T> doesn't. That's the reason.
DataBinding is lazy. If you don't tell your view that something has changed, it won't bother updating. Under the hoods, WPF DataBinding registers for change notifications, so that your ViewModel can tell the view when it has changed. It does this with interfaces like INotifyPropertyChanged and INotifyCollectionChanged.
ObservableCollection<T> implements the interface INotifyCollectionChanged. This interface defines the event CollectionChanged, which your View basically attaches its own event handler to. That handler will update the view when the event is raised by the collection.
I am starting with a new project based on MVVM approach.I will be loading some data asynchronously from some web service.I am confused about where to implement this INotifyPropertyChanged interface,in a viewmodel,model or both.
Also If I implement INotifyPropertyChanged on a model(let's call it ModelA).Then I create an ObservableCollection<ModelA>,Will not it be redundant as ObservableCollection itself keeps track of any data change made to the list?
ObservableCollection class only updates UI in case of following operations:
Item is added in collection.
Item is deleted from collection.
Collection is clear.
But if any property changes in underlying item i.e. ModelA in your case, it won't be reflected on UI unless your Model class is implementing INotifyPropertyChanged interface and raising PropertyChanged event on property change.
So, as a thumb rule you should implement INPC both at your ViewModel layer and also at Model layer.
Practically you need to implement INotifyPropertyChanged to objects which property will be displayed in UI using data binding. If you bind the UI directly to property of your model then you need to implement it in the model too.
ObservableCollection only notify UI when object added or removed from collection. To get the UI notified when property of one or more objects in that ObservableCollection changed you still need to implement INotifyPropertyChanged in that object class.
The answer is simple. If you need your model to implement INotifyPropertyChanged then it should. Redundant or not if you need to keep track on a single property you will be better of using that event rather than the ObservableCollection.
I have a C# Windows Phone 7.1 project that uses the MVVM Light toolkit. On one application page in the app I have a Telerik data bound list box that is bound to a property in my view model. At first that property had a return type of:
List<string>
With that return type the property did not show up in the list of candidate elements in the Path list box, when I activated the Create Data Binding dialog box in order to assign the list box's ItemsSource property. However, when I changed that property's return type to:
ObservableCollection<string>
it showed up immediately. In the future, how can I determine the correct return type for a particular control's ItemsSource or other data bindable property?
Your question is little confusing. Both are completely okay and work well for different scenarios. You use list<string> when you don't plan to change collection, and ObservableCollection otherwise.
Now, you can use MSDN(see ItemsSource property to see what property needs to be what.
As you can see from documentation, ItemsSorce has to be IEnumerable, meaning all types that derive from it, can be used.
Now as for the real question, why didn't your property show up in the IDE, I can only guess that Microsoft wants you to use ObservableCollection always, but I don't agree with this. So that's why it's best to manually do the bindings to properties through Xaml.
in my Silverlight 4 app, I have a class which contains a list of objects. As I want to bind this list to a listbox and notify the listbox about changes to the list, I use an ObservableCollection<>.
I know want to control the adding and deleting of the items in the ObservableCollection by returning an IEnumerable<> and Add/Remove methods in my class.
But now there is no automatic change notification for the listbox to which the list is bound. How can I achieve the automatic notification of the listbox, when my list is returned as IEnumerable (or any other type that would suit the needs)
Thanks in advance,
Frank
Your class should implement the INotifyCollectionChanged interface, since you're implementing the Add and Remove methods in your class, you can raise the notification events.
There is a list for this: ReadOnlyObservableCollection<>
I have an object which has properties. These are wired up with INotifyPropertyChanged. I also have collection properties which implement INotifyCollectionChanged. However, there is no event fired if an item already in a collection is altered.
I would like an event I can use to signal that there has somewhere been a change inside the object - is there one existing already? (it's fairly easy to do by hand, but more consitent if something already exists).
I know the System.Collections.ObjectModel namespace has an ObservableCollection that does this, is that what you're going for?