RaisePropertyChanged should not raise event for string.Empty


To raise all PropertyChanged Events of a ViewModel in one time I can use string.Empty for the property name in PropertyChangedEventArgs.
Since I use GalaSoft.MvvmLight.ViewModelBase.RaisePropertyChanged of MVVM Light, I get the runtime error: Message=Property not found ParamName="" Source=GalaSoft.MvvmLight.WPF4 This is because MVVM Light check all Property-Names.

I would prefer to change VerifyPropertyName in ViewModelBase
to something like this code:
    /// <summary>
    /// Verifies that a property name exists in this ViewModel. This method
    /// can be called before the property is used, for instance before
    /// calling RaisePropertyChanged. It avoids errors when a property name
    /// is changed but some places are missed.
    /// <para>This method is only active in DEBUG mode.</para>
    /// </summary>
    /// <param name="propertyName"></param>
    public void VerifyPropertyName(string propertyName)
        var myType = this.GetType();
        if propertyName <> String.empty
            if (myType.GetProperty(propertyName) == null)
                throw new ArgumentException("Property not found", propertyName);
I think this is a common issue.

Thanks Peter
Closed Oct 13, 2014 at 12:53 PM by lbugnion


Obalix wrote Feb 16, 2011 at 2:54 PM

This code prevents raising notifying the view that it should resync all properties of a view model. Either passing null or an empty sting into the RaisePropertyChanged method will cause an error in DEBUG mode. This was confirmed here http://mvvmlight.codeplex.com/discussions/212293 as a bug. However, the current version still contains the same code.

yadyn wrote Feb 25, 2011 at 7:28 PM

Fix is really simple, too:

public void VerifyPropertyName(string propertyName)
if (String.IsNullOrEmpty(propertyName))
var myType = this.GetType();
if (myType.GetProperty(propertyName) == null)
    throw new ArgumentException("Property not found", propertyName);

You could also optionally put the check in the RaisePropertyChanged method, but this has the benefit of avoiding the check entirely if DEBUG is not set.

Obalix wrote Mar 9, 2011 at 9:55 AM

@yadyn: It is no problem to fix ist, however, this requires a change of the ViewModelBase and, therefore, generates a branch, so my intention is to get it fixed in the main branch. An alternative fix would be to allow inheriting classes access to the property changed handler so that they can implement their own special RaisePropertyChanged methods for special cases.

lbugnion wrote May 16, 2011 at 9:08 AM

Will fix this in the next version, sorry for the bother.

acsed wrote Sep 5, 2011 at 4:11 PM

It's still not fixed. Any quick workaround that doesn't need to modify the code (as we use a compiled DLL) would be very useful. Thx

acsed wrote Sep 6, 2011 at 10:04 AM

Okay, as a quick fix that doesn't involve the code of MVVM Light Toolkit, it's possible to refresh all bindings of a view by "resetting" its datacontext, i.e.

object currentContext = this.DataContext;
this.DataContext = null;
this.DataContext = currentContext;

But as it must be executed by the view, it requires the view model to raise an event, so it would be VERY useful a way more simple to be able to raise property change for the string.Empty value.

lbugnion wrote Sep 6, 2011 at 10:43 AM

The issue is fixed in V4 beta 1. I am currently in the process of writing the release notes and will publish very soon. In the meantime, I pushed the installer to Skydrive:

The MSI will stay available there until I push it to Codeplex. This will be the same file, so feel free to use it already. Let me know if you find issues.


acsed wrote Nov 22, 2011 at 11:50 AM

Now works great, thanks !