Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange

StackOverflow https://stackoverflow.com/questions/22852326

  •  27-06-2023
  •  | 
  •  

Domanda

When I am trying to using NotifyOfPropertyChange() to update UI with the following codes:

public string Username    
{         
    get { return _Username;}  

    set {
        _Username = value;

        NotifyOfPropertyChange(() => Username);
        NotifyOfPropertyChange((x) => CanLogOn(x));
    }
}

public bool CanLogOn(object parameter)
{
    return !string.IsNullOrEmpty(Username); 
}

Intellisence shows the error: (BTW, System.Linq has been referenced)

Cannot convert lambda expression to type 'string' because it is not a delegate type

I'm new to C# and CM, please help me out.

È stato utile?

Soluzione

Looking at the documentation for NotifyOfPropertyChange, you need to only return a property. The framework uses reflection to get the actual string to pass to "PropertyChanged"

By returning a string, you are breaking this system (and this would never work with normal INoifyPropertyChanged either).

You need to notify an actual property that returns the result of "CanLogOn", not try to notify a non-existent property.

Altri suggerimenti

Without seeing it, I'm guessing your NotifyOfPropertyChange works like the RaisePropertyChanged in Microsoft.Prism.ViewModel.NotificationObject.

What you can do instead is add a bool property that returns the result of CanLogOn(UserName) and notify on that as well.

public string Username    
{         
    get { return _Username;}  

    set {
        _Username = value;

        NotifyOfPropertyChange(() => Username);
        NotifyOfPropertyChange(() => Can());
    }
}

public bool Can
{
    get { return CanLogOn(Username); }
}

public bool CanLogOn(object parameter)
{
    return !string.IsNullOrEmpty(Username); 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top