Sneha S Murthy
Sneha S Murthy

Reputation: 141

Silverlight MVVM moving the results from Child Window ViewModel to Parent Window ViewModel

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

Answers (2)

Laurent Le Houerou
Laurent Le Houerou

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

devdigital
devdigital

Reputation: 34349

You can use (in increasing order of decoupling):

  1. As you have a reference to the ChildPage in MainPage, you can access its properties.
  2. Use standard .NET events, where the event is on the child page, and the subscribing is done in the MainPage
  3. Use an event aggregator pattern. Several MVVM frameworks implement the event aggregator pattern.

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

Related Questions