子视图模型如何与Caliburn.Micro中的父视图模型共享DataGrid.selectedItem信息?
答案 0 :(得分:0)
您可以为此使用Event Aggregators。在当前方案中,您可以首先声明一个Message对象,该对象将用于在子视图模型和父视图模型之间传递信息。
public class SelectedItemChangedMessage<T>
{
public T SelectedItem;
}
要发布消息,可以使用EventAggregator类。例如,您可以通过ChildViewModel执行以下操作。
public class ChildViewModel
{
private IEventAggregator _eventAggregator;
public ChildViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public void Change()
{
_eventAggregator.PublishOnUIThread(new SelectedItemChangedMessage<string>(){ SelectedItem = selectedItem });
}
}
然后,在您的ParentViewModel中,您需要订阅EventAggregator
public class ShellViewModel:Screen, IHandle<SelectedItemChangedMessage<string>>
{
private IEventAggregator _eventAggregator;
public ShellViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
public void Handle(SelectedItemChangedMessage<string> message)
{
Debug.WriteLine($"Item Changed, current selection : {message}");
}
}