Question

I am using the Telerik RadRibbonView in my WPF 4.5 project. The set up looks like this.

In my Shell I have a RibbonView and a TabControl defined as a regions called “RibbonRegion” and “TabRegion”. The RibbonRegion is basically the menu of the application and the TabRegion holds the main content.

I have also created a module with a View containing a RibbonTab and a RibbonButton. This button is hocked up to a command that sets the DataContext of a RibbonContextTabView and a TabItemView and registers them in their respective regions. The ContextTab and the TabItem is sharing the same ViewModel. This ViewModel has a propery “IsSelected” that the ContextTab and TabItem are bound to.

    if (_regionManager.Regions["RibbonRegion"].Views.Any(v => v.GetType() == typeof(ContextTabView)) && _regionManager.Regions["TabRegion"].Views.Any(v => v.GetType == typeof(TabItemView)))
    {
        _regionManager.RequestNavigate("RibbonRegion", new Uri("ContextTabView", UriKind.Relative));
        _regionManager.RequestNavigate("TabRegion", new Uri("TabItemView", UriKind.Relative));
    }
    else
    {
        ContextTabView contextTabView = _container.Resolve<ContextTabView>();
        TabItemView tabItemView = _container.Resolve<TabItemView>();
        contextTabView.DataContext = tabItemView.DataContext = new ContextTabTabItemViewModel();
        _regionManager.RegisterViewWithRegion("RibbonRegion", () => contextTabView);
        _regionManager.RegisterViewWithRegion("TabRegion", () => tabItemView);
    }

The first time the Command above is executed the DataContext of the views is set and then they are registered in the regions. This also sets the “IsSelected” property to true. If I change focus to the RibbonTab my ContextTab and TabItem loses focus and the “IsSelected” propery is set to false. If I press the button again the RequestNavigate is executed and once again the property is set to true. Here is my problem. If I do this a third time nothing happens! The RequestNavigate is executed but the property is not set to true and the Views does not regain focus. I am fairly new to PRISM and I am afraid that I am way off here. Any help would be appreciated.

Was it helpful?

Solution

In order to keep communication between ViewModels in a loosely coupled manner, you could simply use the EventAggregator and raise an event from the Command Button implementation, which would be then handled by the TabItemViewModel.

The solution you mentioned by adding one ViewModel into another would not be ideal as these components would end up working with tight coupling and defining an incorrect situation as Views/ViewModels would not depend on another View.

Therefore, to accomplish the EventAgregation approach, you would need to receive the EventAggregator from the container throw constructor on the View/ViewModel where the button is clicked, and on each one of the ViewModels you would want to subscribe to that event setting the IsSelected property inside the EventHandler method.

You could subscribe to the "GiveFocusEvent" event and handle it on the ViewModels which would set their IsSelected property as shown below:

public TabItemViewModel(IEventAggregator eventAggregator, ..){
    ...
    GiveFocusEvent setFocusEvent = eventAggregator.Get<GiveFocusEvent>();
    setFocusEvent.Subscribe(SetFocusEventHandler, ThreadOption.UIThread);
}

public void SetFocusEventHandler(){
     // change IsSelected property value..
}

The Event would be published from inside the Button's CommandHandler method as follows:

this.eventAggregator.GetEvent<GiveFocusEvent>().Publish();

Notice that you would need to create and make your "GiveFocusEvent" event class inherit from CompositePresentationEvent:

public class GiveFocusEvent : CompositePresentationEvent<string>{}

I hope this helped you,

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top