gpt4 book ai didi

c# - 将属性从子 ViewModel 返回到父 ViewModel

转载 作者:行者123 更新时间:2023-11-30 22:17:45 26 4
gpt4 key购买 nike

在我的 WPF MVVM 应用程序中,使用 Caliburn.Micro,我有一个 ViewModel,CreateServiceViewModel,在单击按钮时,它会在单独的窗口中打开一个 GridView,供用户从中选择行。

我为此创建了另一个 ViewModel,MemberSearchViewModel,它有两个属性:

    private Member selectedMember;

public Member SelectedMember
{
get { return selectedMember; }
set { selectedMember = value; }
}

private IList<Member> members;

public IList<Member> Members
{
get { return members; }
set { members = value; }
}

如何将该 SelectedMember 值返回给调用 ViewModel?该 ViewModel 具有 Service.SelectedMember 的属性。

最佳答案

EventAggregator 是您可以使用的...我确信是众多解决方案之一。

public class MessageNotifier{
public object Content{get;set;}
public string Message {get;set;}
}


//MEF bits here
public class HelloWorldViewModel: Screen, IHandle<MessageNotifier>{
private readonly IEventAggregator _eventAggregator

//MEF constructor bits
public YourViewModel(IEventAggregator eventAggregator){
_eventAggregator = eventAggregator;
}

public override OnActivate(){
_eventAggregator.Subscribe(this);
}
public override OnDeactivate(){
_eventAggregator.UnSubscribe(this);
}

//I Handle all messages with this signature and if the message applies to me do something
//
public void Handle(MesssageNotifier _notifier){
if(_notifier.Message == "NewSelectedItem"){
//do something with the content of the selectedItem
var x = _notifier.Content
}
}
}

//MEF attrs
public class HelloWorld2ViewModel: Screen{
private readonly IEventAggregator _eventAggregator
//MEF attrs
public HelloWorld2ViewModel(IEventAggregator eventAggregator){
_eventAggregator = eventAggregator;
}

public someobject SelectedItem{
get{ return _someobject ;}
set{ _someobject = value;
NotifyOfPropertyChange(()=>SelectedItem);
_eventAggregator.Publish(new MessageNotifier(){ Content = SelectedItem, Message="NewSelectedItem"});
}
}

关于c# - 将属性从子 ViewModel 返回到父 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16590119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com