【发布时间】:2019-10-07 10:11:39
【问题描述】:
我想弄清楚如何使用 ViewModel 更新我的布尔属性
INotifyPropertyChanged?
基本上在我的 ViewModel 中,我传入一个字符串列表。每个布尔属性检查列表以查看是否有 字符串值存在。
现在在我的软件生命周期中,列表将得到更新,而我想更新每个属性 使用 INotifyPropertyChanged。
我的问题是如何从 AddToList 方法调用 INotifyPropertyChanged?正在为此使用一种方法
方向正确吗?
public class ViewModel : INotifyPropertyChanged
{
private List<string> _listOfStrings;
public ViewModel(List<string> ListOfStrings)
{
_listOfStrings = ListOfStrings;
}
public bool EnableProperty1 => _listOfStrings.Any(x => x == "Test1");
public bool EnableProperty2 => _listOfStrings.Any(x => x == "Test2");
public bool EnableProperty3 => _listOfStrings.Any(x => x == "Test3");
public bool EnableProperty4 => _listOfStrings.Any(x => x == "Test4");
public void AddToList(string value)
{
_listOfStrings.Add(financialProductType);
// Should I call the OnPropertyChanged here
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
标签: c# viewmodel mvp inotifypropertychanged