Search code examples
wpfmvvmbindingtabitem

Bind an ICommand to a TabItem WPF (MVVM)


I have a TabControl with a few TabItems. I want one of my TabItems to act as a button. When I click on the TabItem, I want it to execute a Command in my associated ViewModel. I have the following code in my View:

         <TabItem  Header="Manage Users" Visibility="{Binding IsAdmin, Converter={StaticResource VisibilityOfBool}}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <i:InvokeCommandAction Command="{Binding Path=OpenLoginCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TabItem>

The OpenLoginCommand is an ICommand in the ViewModel. I have the interactivity namespace defined. What am I missing here?


Solution

  • Try PreviewMouseLeftButtonDown

    <TabItem  Header="Manage Users" Visibility="{Binding IsAdmin, Converter={StaticResource VisibilityOfBool}}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                <i:InvokeCommandAction Command="{Binding Path=OpenLoginCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TabItem>