This is a migrated thread and some comments may be shown as answers.

Notify ViewModel when RadTabControl selection changes (get viewmodel of selected tabitem)

2 Answers 324 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Kalle
Top achievements
Rank 1
Kalle asked on 12 Mar 2013, 06:19 PM
Hi!

I'm using RadTabControl as a Prism region and Tab items are successfully created. I have a Shell window which has a RadTabControl. Can I somehow get the view/viewmodel of the selected TabItem (tab's content) when TabControl's selected item changes so that I could call a method of that view model?

All of this is because I'm using PRISM RegionNavigationService in the Shell's viewmodel and I want each tab to be able to publish their NavigationService via EventAggregator event when tab is selected.

Other ways of doing this are also accepted of course :)

Br,

Kalle

2 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 15 Mar 2013, 03:06 PM
Hi Kalle,

The RadTabControl provides a SelectionChange event which you can handle to get all selected or unselected items through the arguments of the event (using the AddedItems and RemovedItems collections). Also, if the RadTabControl is populated with business data (if you're using PRISM to inject ViewModels, rather than Views), then you can bind the RadTabControl SelectedItem property to a property from the Shell ViewModel - if you use TwoWay binding, you will be able to handle any changes in the selection and receive the business object that is currently selected.

I hope this information will help you find a working approach for your case. And if you need further assistance, you can send a sample solution demonstrating your scenario and we will gladly take a closer look at your implementation and suggest other approaches.

Kind regards,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Kalle
Top achievements
Rank 1
answered on 16 Mar 2013, 03:29 PM
Hi!

I was able to solve this problem myself using IsSelected property of the TabItem.
I created a property IsTabSelected to DashboardTabViewModel and bind that to TabItem's IsSelected property and after that get the navigation service from scoped region manager only when IsTabSelected == true and publish it in an event. 

private bool bIsTabSelected;
public bool IsTabSelected
{
    get { return bIsTabSelected; }
    set
    {
        bIsTabSelected = value;
        RaisePropertyChanged("IsTabSelected");
        if(value == true)
            SendNavigationServiceEvent();
    }
}
 
private void SendNavigationServiceEvent()
{
    if (this.RegionManager == null) return;
 
    // Send event with Navigation service to handle back/forward navigation under this Tab
    var dashboardEvent = this.eventAggregator.GetEvent<DashboardOpenTabEvent>();
    DashboardTabOpenParams eventParams = new DashboardTabOpenParams();
    eventParams.NavigationService = this.RegionManager.Regions[RegionNames.TabContentRegion].NavigationService;
    dashboardEvent.Publish(eventParams);
}

This seems to be good enough way of doing this at least for now, of course I'm accepting other ways for possible improvement :)

Br,

Kalle
Tags
TabControl
Asked by
Kalle
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Kalle
Top achievements
Rank 1
Share this question
or