Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TabControl change TabItem with Command

Tags:

command

mvvm

wpf

I have a WPF application and in the main window I have a TabControl. I would like the TabItems to fire a RelayCommand that I have in my ViewModel when they get selected (different commands for each TabItem). I don't want a TabItem to be selected for some conditions that are set in the commands CanExecute function. Is this possible?

like image 990
Peter Avatar asked Nov 29 '12 08:11

Peter


1 Answers

I think the right way to go here is not 'ICommand' but just a ViewModel property which will twoway bind to the TabControl.SelectedIndex (or SelectedItem) then you can take the decision in the ViewModel. This is better than defining a lot of commands specific to each TabItem

<TabControl SelectedIndex="{Binding VMSelectedTabIndex, Mode=TwoWay}">
//OR
<TabControl SelectedItem="{Binding VMSelectedItem, Mode=TwoWay}">
like image 161
Jobi Joy Avatar answered Sep 30 '22 20:09

Jobi Joy