David
David

Reputation: 1293

Raise an event from Child View Model

I have a setup as follows.

Thats because I wan't to use the SearchView and ResultsView in different parts of my application

My ContainerViewModel has a handle to the other VM's like

SearchViewModel searchbViewModel = new SearchbViewModel();
ResultsViewModel resultsViewModel = new ResultsViewModel();

Each View Model has their own DataContext

I want to be able to raise an event from the SearchViewModel to the ContainerViewModel to let it know a search has been performed.

This is what I have tried:

ContainerViewModel

searchJobViewModel.OnSearchPerformed += SearchJobViewModel_OnSearchPerformed;

public void SearchJobViewModel_OnSearchPerformed()
{

}

SearchViewModel

public delegate void SearchPerformed();
public SearchPerformed OnSearchPerformed { get; set; }

public void Execute_SearchJobs()
{
  if (OnSearchPerformed != null) 
    OnSearchPerformed();
}

When I hit the search button and the Execute_SearchJobs method fires OnSearchPerformed is always null

What am I missing?

Upvotes: 0

Views: 338

Answers (1)

nvoigt
nvoigt

Reputation: 77294

Does that even compile? I think what you want is an event:

public event SearchPerformed OnSearchPerformed;

Why your eventhandler is null is probably because the code that added a receiver to the event was not called yet or was called on a different instance of the class. You will need to debug that behaviour or post more code here.

Upvotes: 1

Related Questions