Simsons
Simsons

Reputation: 12745

How to Update View and View Model without using INotifyPropertyChanged

When we have a twoway binding involved between View and View-Model, we implement the INotifyPropertyChanged interface which has the following event:

// Summary:
//     Occurs when a property value changes.
event PropertyChangedEventHandler PropertyChanged;

But can I do the same without implementing INotifyPropertyChanged?

Upvotes: 2

Views: 406

Answers (4)

Peter Ritchie
Peter Ritchie

Reputation: 35870

If you can't implement INotifyPropertyChanged (INPC) (or don't have the source to what you want to bind to) then you should wrap whatever it is you want to bind to in your View Model and the View Model would implement INPC for the binding and have to to get/set data to, presumably, that other class.

Upvotes: 2

dowhilefor
dowhilefor

Reputation: 11051

You can of course. You simply loose the easy way to update the view when the viewmodel is changed. Now there are ways to overcome that.

  1. Reapply the DataContext, that way all bindings gets evaluated again, but this is very expensive.
  2. In the ui, get the binding of the desired property and call UpdateTarget.

But obviously these are all very strange workarounds. Its also worth mentioning, that binding to an object that doesn't implement INotifyPropertyChanged is usually slower.

Upvotes: 1

blindmeis
blindmeis

Reputation: 22445

i would say no. you have to implement INotifyPropertyChanged in any way, otherwise binding with twoway would not work.

Upvotes: 0

user128300
user128300

Reputation:

If you want to avoid implementing INotifyPropertyChanged explicitly, you could use a code-weaving tool like PostSharp. Then, implementing INotifyPropertyChanged becomes as simple as

[NotifyPropertyChanged]
public class Shape
{
    public double X { get; set; }
    public double Y { get; set; }
}

You can find more details here: http://www.sharpcrafters.com/solutions/notifypropertychanged# .

Upvotes: 1

Related Questions