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

3 posts, 0 answers
  1. Kalle
    Kalle avatar
    26 posts
    Member since:
    Feb 2013

    Posted 12 Mar 2013 Link to this post

    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. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 15 Mar 2013 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Kalle
    Kalle avatar
    26 posts
    Member since:
    Feb 2013

    Posted 16 Mar 2013 Link to this post

    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
Back to Top