I am writing an application using WPF, MVVM, and PRISM regions.
I have two regions with one view (A and B) per region. I add views to my regions in my regionmodules class as follows:
// I use the following private properties in both regionmodules.cs class
private readonly IUnityContainer _container;
private readonly IRegionManager _regionManager;
--A--
private void RegisterViews()
{
var controlsView = _container.Resolve<EmulatorControlsView>();
_regionManager.Regions["ControlsRegion"].Add(controlsView, "A");
}
--B--
private void RegisterViews()
{
var templatesView = _container.Resolve<TemplatesView>();
_regionManager.Regions["TemplatesRegion"].Add(templatesView, "B");
var view = _regionManager.Regions["TemplatesRegion"].GetView("B");
_regionManager.Regions["TemplatesRegion"].Deactivate(view);
}
I deactivate view "B" during initialization of my app because view B overlays view A in my shell.xaml.
A click command from view A will activate view B and deactivate view A per the following code:
private void LoadTemplateExecute()
{
var controlView = _regionManager.Regions["ControlsRegion"].GetView("A");
_regionManager.Regions["ControlsRegion"].Deactivate(controlView);
var templateView = _regionManager.Regions["TemplatesRegion"].GetView("B");
_regionManager.Regions["TemplatesRegion"].Activate(templateView);
}
Other button commands exist in my Templates region view model that Deactivates view A and Activates view B.
This code works great, and allows me to display my views on demand.
Problem: View B has a ListView bound to an ObservableCollection in the view model via dependency property. I currently add string items to my ObservableCollection by way of the view model constructor:
private void InitializeTemporaryLists()
{
TemplateList.Add("Dog");
TemplateList.Add("Cat");
TemplateList.Add("Horse");
}
However, when view B is activated, my ObservableCollection does not show the items in its list.
Question: Is there an event available in the region view or view model to notify me when the view has become active?
What is the best way for me to populate data to my view from the view model each time my view becomes active?
Any ideas and code example are very much appreciated!
* Update * My ListView was not updating because I am a silly programmer. I had not declared the ObservableCollection<> type in my dependency property.
However, I am still curious to know how you smart programmers out there might handle a prism region view activation? I wish I could subscribe to an event like the following:
_regionManager.Regions["YourRegion"].OnActiveView() += HandleActiveViewEvent;