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
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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); 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top