This project has moved and is read-only. For the latest updates, please go here.
8

Closed

RaisePropertyChanged should not raise event for string.Empty

description

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>
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    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 1:53 PM by lbugnion

comments

Obalix wrote Feb 16, 2011 at 3: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 8:28 PM

Fix is really simple, too:

public void VerifyPropertyName(string propertyName)
{
if (String.IsNullOrEmpty(propertyName))
    return;
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 10: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 10:08 AM

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

acsed wrote Sep 5, 2011 at 5: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 11: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.
Thx

lbugnion wrote Sep 6, 2011 at 11:43 AM

Hi,
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:
https://skydrive.live.com/redir.aspx?cid=40cffde85f1ab56a&resid=40CFFDE85F1AB56A!845

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.

Thanks,
Laurent

acsed wrote Nov 22, 2011 at 12:50 PM

Now works great, thanks !