Silverlight MVVM 将结果从子窗口 ViewModel 移动到父窗口 ViewModel
您好,我有一个使用 MVVM light 的 silverlight MVVM 应用程序。 当我打开应用程序时,应弹出一个子窗口,在子窗口中指定条件并单击“确定”按钮后,主窗口应显示详细信息。
public MainPage()
{
ChildPage cp = new ChildPage();
cp.Show();
InitializeComponent();
}
单击子窗口上的“确定”按钮后,该窗口应消失并在主窗口上显示对象列表。在子窗口的视图模型中,我有一个 RelayCommand OKCommand。
private void WireCommands()
{
OKCommand = new RelayCommand(GetEmployees);
}
private void GetEmployees()
{
IEnumerable<Employees> employees;
employees = from employee in Employees where employee.Name == selectedEmployee.Name select employee;
Employees= new ObservableCollection<Employee>(employees);
}
员工获得了所需的结果。但我不知道如何关闭子窗口并将结果移动到父窗口。提前致谢。
Hi I have a silverlight MVVM application using MVVM light.
When I open the application a child window should popup and upon specifying the condition in the child window and clicking OK button the main window should display the details.
public MainPage()
{
ChildPage cp = new ChildPage();
cp.Show();
InitializeComponent();
}
upon hitting OK button on the child window this window should disappear and display a list of objects on the main window. In the View Model of the child window I have a RelayCommand OKCommand.
private void WireCommands()
{
OKCommand = new RelayCommand(GetEmployees);
}
private void GetEmployees()
{
IEnumerable<Employees> employees;
employees = from employee in Employees where employee.Name == selectedEmployee.Name select employee;
Employees= new ObservableCollection<Employee>(employees);
}
The Employees has the required result. But I dont know how to close the chils window and move the result to the parent window. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用(按解耦的递增顺序):
使用 .NET 事件
使用 Caliburn.Micro 的事件聚合器
这里,我们在从 ChildPage 接收到姓名后,在 MainPage 中进行员工检索。或者,您可以检索 ChildPage 中的员工,并将它们传递到事件参数/消息中。
You can use (in increasing order of decoupling):
Using .NET Events
Using Event Aggregator from Caliburn.Micro
Here we are doing the employee retrieval in the MainPage, after receiving the name from the ChildPage. Alternatively, you could retrieve the employees in the ChildPage, and pass them in the event args/message.
要关闭子窗口,您可以使用 ChildWindow 的 Close() 方法,也可以将 DialogResult 属性设置为 true 或 false,这也会将其关闭。
您必须在 ChildPage 的 OK 按钮的 OnClick 事件的隐藏代码中执行此操作。
要访问 ChildPage 的 ViewModel 的 Members 属性,您可以执行类似的操作:
To close the child window, you can either use the Close() Method of the ChildWindow or you can set the DialogResult property to true or false which also close it.
You have to do it in the code-behind of ChildPage on the OnClick event of the OK Button.
To access the Employees property of the ChildPage's ViewModel you can do something like that :