【发布时间】:2019-09-26 08:00:54
【问题描述】:
我之前类似相关的question 是通过使用INotifyPropertyChanged 回答的。然而,研究告诉我,从GalaSoft.MvvmLight 继承ViewModelBase 与INotifyPropertyChanged 相似。
我在更改ObservableCollection 中每个项目的数据时使用了我的问题中的this answer。但我不想再使用INotifyPropertyChanged,因为我已经继承了ViewModelBase。下面的代码是我从已经提到的答案中添加的一些代码:
食品类
private bool _isAllSelected = true;
public bool IsAllSelected
{
get
{
return _isAllSelected;
}
set
{
Set(IsAllSelected, ref _isAllSelected, value);
// send message to viewmodel
Messenger.Default.Send(Message.message);
}
}
ViewModel 类
// message handler
private void MsgHandler(Message message)
{
RaisePropertyChanged(SelectAllPropertyName);
}
// the property that change all checkbox of fruits
public const string SelectAllPropertyName = "SelectAll";
public bool SelectAll
{
set
{
bool isAllSelected = Foods.Select(c => c.IsAllSelected).Any();
foreach (var item in Foods.SelectMany(c => c.Fruits).ToList())
{
item.IsSelected = isAllSelected;
}
}
}
// receives message function, called at the start
public void Receiver()
{
Messenger.Default.Register<Message>(this, MsgHandler);
}
这里的问题是它不像以前使用 INotifyPropertyChanged 那样工作。
【问题讨论】:
-
您在哪里收到您的消息?
-
我有一个接收消息的函数。见编辑
-
什么时候调用receiver方法?如果从不调用此方法,则您将永远不会订阅此事件。这个方法真的按预期调用了吗?
-
当然如前所述。
标签: c# mvvm mvvm-light