MVVM Update binding issue - wpf

I have user control to which I bind a viewmodel, this implements the INotifyPropertyChanged, thru the datacontext, this has a property that is a IList that I bind to the itemsdatasource of a grid, then on the code, in another class I add some values to the list, but the UI doesn't reflect this change although in debug I can see that the datagrid has this items added but they don't appear in the UI, can anybody help me, I can't see what is the problem.

ObservableCollection<T> is your friend. It implements INotifyCollectionChanged, saving you the trouble.

Related

List<> collection does not update the View in MVVM

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.

Refresh a DataGrid in the MVVM pattern

I have a View with a dataGrid. This datagrid bind a property in the ViewModel that is an ObservableCollection.
I edit some data in the dataGrid, and a field is updated by code, because it depends on some operations. Well, if I check the item in the observable collection, I can see that all the data is correct, but the info in the dataGrid is no refresh.
I want to force the refresh because I know that the observableCollection only raise the change property event when I add o remove items, but not if I edit one of them.
Because I am use Entity Framework 4.1, really the ItemsSource of the dataGrid is the local of the DbSet, so I don't know how to implement the notifyPorpertyChanged in the classes of the model edmx, and I am looking for an alternative, like to force refresh the dataGrid.
Because the property of the ViewModel that I use to bing the ItemsSource of the dataGrid is a reference to the local, I mean that to set the property I do myProperty = myContext.MyTable.Local and that raise the event PropertyChanged that I implement in my ViewModel, I try to do myProperty = myContext.MyTable.Local again to try to raise the event and force the refresh of the dataGrid, but it does not work.
What alternatives do I have?
Make sure that you have the Binding Mode set to TwoWay. Implement in the set portion of your property OnPropertyChanged and the rest should take care of itself.

INotifyPropertyChanged or INotifyCollectionChanged with DataTable?

Hi i am having some troube with DataTables. So What i need is to detect whenever i change any cell in the DataGrid of the DataTable that is binded.
How to do it? With INotifyPropertyChanged or with INotifyCollectionChanged?
Note: I am trying with INotifyPropertyChanged but it only detects when i set some value in the DataTable, and never when i change any value of any cell in the DataGrid, i already have tried OneWay and TwoWay but nothing happens.
Thanks in advance!
The datagrid would be bound to a list of objects. If you want the grid to update when individual object properties change, than each contained object must implement the INotifyPropertyChanged interface.
The INotifyCollectionChanged is an interface that the collection should implement, and are for notifications of addition and removal events.
See the section "How to implement collections" on this page.
Here's a way to approach your problem:
Create a new class that exposes the properties currently held in each DataRow. On this class implement INotifyPropertyChanged.
Instead of a DataTable, use an ObservableCollection<T> or your new class.
ObservableCollection already implements INotifyCollectionChanged, so this saves you the effort of implementing it yourself.
if you set the itemssource of your datagrid to a datatable then wpf create a IBindingListView wich is bound to the datagrid.
what you can do now is edit,add and delete items to your datatable via datagrid. if you wanna know when a cell in your datatable is changed you can subscribe to DataTable.ColumnChanged event.
why do you want do know when a cell is changed?
The answer to the title of your question is : Neither. Actually you do not need to bind a DataTable to a DataGrid. You bind a DataView. "The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for."(Binding Sources Overview)
One answer to your question is :
You modify the datagrid cell with a TextBox (usually). Do this with a new textbox inheriting from TextBox and override the OnGotFocus and OnLostFocus methods of it.

How to implement INotifyPropertyChanged and observableCollection in MVVM pattern?

I have an ObservableCollection of Products in the Model, and I want the ViewModel to listen to any changes in the ObservableCollection of Products.
Im not sure about how to go and implement it. Ive read several tutorials, but most of them are not MVVM specific.
Should i implement the INotifyPropertyChanged in the ViewModel class, how do I specify that i want to listen to the OberserableCollection of Products?
Thanks :)
I know this has already been answered, but there is more to consider.
1) a ViewModel should implement INotifyPropertyChanged, that's one of the things that makes it a ViewModel. Even in the unlikely case that the only property it exposes is the ObservableCollection, it will need to raise property changed when the actual ObservableColelction property is changed (more on this in item 3)
2) do you really want the ViewModel to listen for these changes or the View? Those are two different things. The ViewModel should hold an ObservableCollection which is then bound to the View. What you want is for the View to react to those changes. In that case, Brandon is correct that ObservableCollection does this for you out of the box. So the View has an instance of the ViewModel set as a DataContext and some visual element in your View is bound to the ObservableCollection (like an ItemsSource on a ListBox).
3) the exception is the ObservableCollection property itself in the ViewModel. While ObservableCollection implements INotifyPropertyChanged, that is part of the collection object: when that object reference changes (like it is recreated) in the ViewModel, then the ViewModel still needs to report that the Property for the ObservableCollection has changed.
Just some thoughts.
ObservableCollection already implements INotifyPropertyChanged, so you don't need to.

WPF - Combobox SelectedItem not getting set?

I have a ComboBox that has its ItemsSource bound to a static List<CustomSettings> of options. The ComboBox is part of a form which is bound to a CustomObject class, and one of the properties on that class is a CustomSettingProperty.
I would like to bind the SelectedItem of the ComboBox to the property specified in the CustomObject, however SelectedItem="{Binding Path=CustomSettingProperty}" is not setting the default selected item. Using breakpoints I can see that it is calling the get; method, so I think the problem might be in the fact the CustomSettingProperty is created separately from the List<CustomObject> so WPF does not think it is the same item.
Is there an easy way to do this? Or perhaps an alternative since the CustomSettings class does contain an Id?
If the item that is selected is not the same instance that is contained in the List, you must override Equals() in the CustomObject to let the ComboBox know that it is the same object.
If it's the same instance, maybe it's only a simple thing such as setting the BindingMode to TwoWay:
SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"
I found the solution, It was The Prism's Event Aggregator was passed with reference type so That the ui thread stops processing

Resources