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

Bind to selected tab

3 Answers 374 Views
TabView
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 05 Feb 2018, 10:42 AM

Hi,

I am developing an X.F. app using MVVM. In my view model I would like to know which tab is selected so I would like to bind the selected tab. I could bind the SelectedItem property but I don't want my view model to have dependency's to Telerik since the view model should have no knowledge about the view. There is no selected index property or something. How can my view model know what tab is selected and how can I set it from the view model without dependency's on Telerik?

Best Regards,
Michel Moorlag

3 Answers, 1 is accepted

Sort by
0
Petar Marchev
Telerik team
answered on 05 Feb 2018, 02:56 PM
Hi Michel,

I agree that you should not have to obtain a reference to the exact TabViewItem in your view model. However, if you did that, what information would that give you? Would you perhaps extract the BindingContext of the tab item? How would you map a tab item to your business object?

My point here is that the TabView does not have a strategy to map business data items to tab view items but you probably do. Then you could extract your business item from the selected TabViewItem and you could send it to your view model. One way to do this is to create a new attached property, say SelectedDataItem. You can then create a two-way binding to a property in your view model:
<telerikPrimitives:RadTabView local:MyAttachedProperties.SelectedDataItem="{Binding SelectedTabItem, Mode=TwoWay}" />

Now you can attach a handler to the property changed and see when the SelectedItem of the TabView changes. Then you can update the attached property to the underlying business item.

I hope I was able to explain well one possible way to achieve your goals. If you need further assistance, do let us know and please share more information on how many tabs you have and what information from each tab you will need in your view models. 

Regards,
Petar Marchev
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Michel
Top achievements
Rank 1
answered on 09 Feb 2018, 08:29 AM

Hi Petar,

Thank you for your input but I found a different solution. I am now binding the IsSelected property of the different tabs. 

So in my VM is have property:

        public bool[] Tabs
        {
            get { return _tabs; }
            set
            {
                _tabs = value;
                RaisePropertyChanged("Tabs");
            }
        }

In the constructor I initialize it like this:

Tabs = new[] {true, false, false, false};

So the first tab I bind like this:  IsSelected="{Binding Tabs[0], Mode=TwoWay}"

Second tab like this:  IsSelected="{Binding Tabs[1], Mode=TwoWay}" etc.

By looking which item is true I can detect what tab is selected.

Thank you for you time.

 

0
Stefan Nenchev
Telerik team
answered on 14 Feb 2018, 07:42 AM
Hello, Michel,

We are glad to see that you have managed to find a way to achieve the requirement you have. I will proceed with closing this ticket now, however, if you notice any issues or undesired effects, do not hesitate to reopen it by simply replying back.

Have a great rest of the week.

Regards,
Stefan Nenchev
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
TabView
Asked by
Michel
Top achievements
Rank 1
Answers by
Petar Marchev
Telerik team
Michel
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or