WPF TabControl modifies TabItem with command

I have a WPF application and in the main window I have TabControl . I would like TabItems run RelayCommand , which I have in my ViewModel when they are selected (different commands for each TabItem ). I do not want TabItem be selected for some conditions set in the CanExecute . Is it possible?

+6
source share
1 answer

I think the correct path here is not β€œICommand”, but just a ViewModel property that will be bound to TabControl.SelectedIndex (or SelectedItem), then you can make a decision in ViewModel. This is better than defining many commands specific to each TabItem.

 <TabControl SelectedIndex="{Binding VMSelectedTabIndex, Mode=TwoWay}"> //OR <TabControl SelectedItem="{Binding VMSelectedItem, Mode=TwoWay}"> 
+3
source

All Articles