2014-02-28 37 views
0

Iam使用MVVMlight for windows 8.1應用程序。我想在點擊列表項目時導航到一個新的視圖,並將點擊的項目作爲參數傳遞。Winrt MVVMLight在listItemClick上導航,帶參數

我已經定義了一個附加屬性,如:

public static class ItemClickCommand 
{ 
    public static readonly DependencyProperty CommandProperty = 
     DependencyProperty.RegisterAttached("Command", typeof(ICommand), 
     typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged)); 

    public static void SetCommand(DependencyObject d, ICommand value) 
    { 
     d.SetValue(CommandProperty, value); 
    } 

    public static ICommand GetCommand(DependencyObject d) 
    { 
     return (ICommand)d.GetValue(CommandProperty); 
    } 

    private static void OnCommandPropertyChanged(DependencyObject d, 
     DependencyPropertyChangedEventArgs e) 
    { 
     var control = d as ListViewBase; 
     if (control != null) 
      control.ItemClick += OnItemClick; 
    } 

    private static void OnItemClick(object sender, ItemClickEventArgs e) 
    { 
     var control = sender as ListViewBase; 
     var command = GetCommand(control); 

     if (command != null && command.CanExecute(e.ClickedItem)) 
      command.Execute(e.ClickedItem); 
    } 
} 

視圖中的XAML的樣子:

<GridView 
         ItemsSource="{Binding Source={StaticResource ItemsSource}}" 
         ItemTemplate="{StaticResource itemsTemplate}" 
         SelectionMode="None" 
         helpers:ItemClickCommand.Command="{Binding ItemClicked}" 
         > 
        </GridView> 

和視圖模型:

private RelayCommand<Item> _ItemClicked; 
    public RelayCommand<Item> ItemClicked 
    { 
     get 
     { 
      if (_ItemClicked == null) 
      { 
       _ItemClicked = new RelayCommand<Item>(
        (item) => 
        { 
         _navigationService.Navigate(typeof(ItemsPage)); 
        }); 
      } 

      return _ItemClicked; 
     }   
    } 

時沒有任何反應單擊網格項目。

+1

當你調試,是'RelayCommand'被稱爲?有沒有輸出?如果導航失敗,它會靜靜地失敗(並返回「False」)。 –

+1

該項目被點擊後,RelayCommand未運行,我通過添加IsItemClickEnabled =「True」 – beesasoh

回答