Reputation: 141
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.
Upvotes: 0
Views: 1078
Reputation: 11
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 :
public MainPage()
{
ChildPage cp = new ChildPage();
cp.Closed += (s,e) =>
{
//Do something with (cp.DataContext as ChildPageViewModel).Employees
}
cp.Show();
InitializeComponent();
}
Upvotes: 0
Reputation: 34349
You can use (in increasing order of decoupling):
Using .NET Events
ChildPage cp = new ChildPage();
cp.NameReceived += NameReceived;
cp.Show();
private void NameRecieved(object sender, NameReceivedEventArgs eventArgs)
{
// retrieve employees using eventargs.Name
}
Using Event Aggregator from Caliburn.Micro
public class MainPage : Screen, IHandle<NameReceivedMessage>
{
public MainPage(IEventAggregator eventAggregator)
{
eventAggregator.Subscribe(this);
}
public void Handle(NameReceivedMessage message)
{
// retrieve employees using message.Name which is the inputted name
}
}
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.
Upvotes: 1