mmrp5
mmrp5

Reputation: 25

how to bind isselected property of tabitem in xaml

I have 5 tabs on my mainwindow.xaml. On each tab there is a button. On button click I have to change the view to next tab.

Can I do this (changing the tabs on button click) in the xaml itself by binding IsSelected property of the TabItem to my previous button command property (I am enabling/disabling the buttons by using commands)?

My CanExecute will check the IsEnabled property of the buttons.

I can do this in the code behind by changing the SelectedTabIndex or SelectedTabItem. But can I do it in the xaml to follow MVVM pattern?

If my question is not a valid question, please ignore it.

Note: My tabs are not from templates.

Upvotes: 2

Views: 1708

Answers (1)

Kylo Ren
Kylo Ren

Reputation: 8813

Use this Style of TabControl:

<Style TargetType="TabControl">
        <Setter Property="SelectedIndex" Value="{Binding SelectedIndex}"/>
</Style>

Bind same Command to all buttons and on every click keep incrementing the SelectedIndex property in your ViewModel.

Property in ViewModel:

private int selectedIndex;

public int SelectedIndex
{
    get { return selectedIndex; }
    set { selectedIndex = value;
    UpdateProperty("SelectedIndex");
    }
}

Command Action:

SelectedIndex++;
if (SelectedIndex == 5)
{
    SelectedIndex = 0;
}

check for SelectedIndex = 5 cause your tab are fixed.else you have to bind count of items in a property of ViewModel and then put a check.

Upvotes: 1

Related Questions