I would like to call a Command when a TabItem of my TabControl is selected.
Is there a way to do it without breaking the MVVM pattern ?
I would like to call a Command when a TabItem of my TabControl is selected.
Is there a way to do it without breaking the MVVM pattern ?
It can be done using the following classes together:
EventTrigger
class from theSystem.Windows.Interactivity
namespace (System.Windows.Interactivity
assembly).EventToCommand
class from theGalaSoft.MvvmLight.Command
namespace (MVVM Light Toolkit assembly, for example,GalaSoft.MvvmLight.Extras.WPF4
):XAML:
Create an instance of the command in the
ViewModel
constructor:Here is how I did it without
GalaSoft.MvvmLight
or Attached Command BehaviorUse an AttachedCommand Behavior, which will let you bind a Command to WPF events
Of course, if you're using the MVVM design pattern and binding
SelectedItem
orSelectedIndex
, you could also run the command in thePropertyChanged
event