Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM - RaisePropertyChanged turning code into a mess

New to MVVM so please excuse my ignorance.

I THINK i'm using it right but I find my ViewModel has too many of these:

RaisePropertyChanged("SomeProperty")

Every time I set a property I have to raise that damned property changed.

I miss the days where I could just go:

public int SomeInteger { get; private set;}

These days I have to stick the "RaisePropertyChanged" in everywhere or my UI does not reflect the changes :(

Am I doing it wrong or are other people getting annoyed with the excessive number of magic strings and old school property setters?

Should I be using dependency properties instead? (I doubt that would help the code bloat anyway)

Despite these problems I still think MVVM is the way to go so I guess that's something.

like image 628
vidalsasoon Avatar asked Mar 31 '10 12:03

vidalsasoon


2 Answers

Take a look at this What is the best or most interesting use of Extension Methods you've seen?.

It describes an extension method and a helper method that my Model and ViewModel classes use to enable the following strongly typed (no magic string) properties.

private string _name;
public string Name
{
    get { return _name; }
    set { this.NotifySetProperty(ref _name, value, () => this.Name); }
}

This is about as simple as I think it can get. Hope it helps.

like image 145
Enigmativity Avatar answered Sep 29 '22 17:09

Enigmativity


You could use PostSharp's NotifyPropertyChanged attribute. Then all you have to do is to put an attribute on the class and that's it. E.g.:

[NotifyPropertyChanged]
public class MyClass 
{
    public string MyProperty { get; set; }
}
like image 21
PL. Avatar answered Sep 29 '22 17:09

PL.