2014-04-01 37 views
2

我正在使用MVVM模板(Caliburn.Micro)製作WP8應用程序。WP8列表框加載MVVM

我使用的ListBox命名的ProgramsList,我想在加載時做一些事情。

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" Loaded=""> 

當不使用MVVM模板時,我可以使用自動生成的事件處理函數。

如何以正確的方式使用MVVM模板來做到這一點?

回答

1

您可以使用命令從視圖模型暴露你的邏輯中,然後使用行爲,例如:http://metroeventtocommand.codeplex.com/

如果不能滿足您的需求,您可以隨時使用的事件處理程序,並從那裏調用命令。

2
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras" 

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" > 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <cmd:EventToCommand Command="{Binding LoadedCommand}" /> 
     </i:EventTrigger>   
    </i:Interaction.Triggers> 
</ListBox> 


    public RelayCommand LoadedCommand 
      { 
       get; 
       private set; 
      } 

      /// <summary> 
      /// Initializes a new instance of the SplashScreenViewModel class. 
      /// </summary> 
      public SplashScreenViewModel() 
      { 
       LoadedCommand = new RelayCommand(toDoSomehing); 
      } 

    private void toDoSomething(){ 
    }