2013-02-16 35 views
1

我發現了很多有關處理後面的代碼中的TabItem頭單擊事件的答案,但我需要在視圖模型中處理該事件。 在此先感謝WPF-MVVM:在視圖模型中的TabItem頭單擊事件處理

+0

http://stackoverflow.com/questions/ 5162805 /綁定-A-的ICommand到一個-WPF-的tabcontrol-TabItem的-使用-XAML的MVVM – kenny 2013-02-16 11:32:31

回答

2

將屬性綁定到選項卡控件SelectedIndex。

您的XAML:

<TabControl x:Name="tabControl" SelectedIndex="{Binding tabControlSelectedIndex}"> 

您的視圖模型:

Private _tabControlSelectedIndex As Integer 
Public Property tabControlSelectedIndex As Integer 
    Get 
     Return _tabControlSelectedIndex 
    End Get 
    Set(value As Integer) 
     If _tabControlSelectedIndex <> value Then 
      _tabControlSelectedIndex = value 
      OnPropertyChanged("tabControlSelectedIndex") 

      ' 
      ' Whatever you want to handle here 
      ' 

     End If 
    End Set 
End Property 
1

您可以使用MVVM光的EventToCommand做法:

  1. 增加提及System.Windows.Interactivity.dll到您的項目。

  2. 添加xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

  3. 添加XAML如:

    <Button> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter" > <i:InvokeCommandAction Command="{Binding FooCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button>

你可以看到這裏的代碼: http://www.danharman.net/2011/08/05/binding-wpf-events-to-mvvm-viewmodel-commands/