2012-01-10 66 views
1

我得到我要離開的Pivot項目的索引,而不是我要去的Pivot項目。
我已經搜索了一段時間,可以想到一些解決方案,比如在視圖中使用事件,然後使用MVVM-Light向ViewModel發送消息。但我寧願不這樣做,我寧願堅持目前的實施,當然略有不同。樞軸控制MVVM-Light-EventToCommand SelectedIndex發送以前的索引

任何幫助表示讚賞

我的XAML:

<controls:Pivot x:Name="ContentPivot" Margin="12,0,12,0"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="SelectionChanged"> 
       <cmd:EventToCommand Command ="{Binding SelectSlideCommand}" 
             CommandParameter="{Binding SelectedIndex, ElementName=ContentPivot}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 

     <controls:PivotItem x:Name="PivotItem0" Header="0"> 
      <Image Source="{Binding ViewingSlides[0].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
     </controls:PivotItem> 

     <controls:PivotItem x:Name="PivotItem1" Header="1"> 
      <Image Source="{Binding ViewingSlides[1].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
     </controls:PivotItem> 

     <controls:PivotItem x:Name="PivotItem2" Header="2"> 
      <Image Source="{Binding ViewingSlides[2].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
     </controls:PivotItem> 
    </controls:Pivot> 

和c#:

public RelayCommand<int> SelectSlideCommand; 

    SelectSlideCommand = new RelayCommand<int>((pivotItem) => SelectSlideAction(pivotItem)); 

    void SelectSlideAction(int parameter) 
    { 
     currentIndex = parameter; 

     UpdateViewingSlides(); 
     Debug.WriteLine("SelectSlideAction: " + parameter); 
    } 

回答

2

你在你的控制有`SelectedItem屬性...你可以把它掛你的ViewModel屬性在TwoWay綁定模式下始終得到更新的值(那麼你不需要這個命令)....你也可以試試

 <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectionChanged"> 
      <cmd:EventToCommand Command ="{Binding SelectSlideCommand}" 
            CommandParameter="{Binding SelectedItem, ElementName=ContentPivot}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
+0

謝謝!我已經嘗試綁定SelectedItem屬性,但我不知道TwoWay綁定模式。現在一切正常!謝謝 – Raymen 2012-01-10 20:18:09

+0

@RaymenScholten使用拖曳方式綁定,您可以選擇ViewModel中的項目以顯示在視圖中和副本中。它在這個senario非常方便.. – Ankesh 2012-01-10 20:20:13