2012-07-24 96 views
2

我已经使用mvvm模式创建了一个longlistselector。使用LongListSelector控件导航

我在不同的标题下创建了许多城市名称。

我想知道如何在用户选择或点击特定项目时导航到特定页面。

回答

2

可以简单地线了的事件侦听器或者在每个ListBoxItemtap事件(即在含有grid/canvas /无论)或在列表本身上SelectionChanged事件。

一个例子:

<ListBox:ItemTemplate> 
    <DataTemplate> 
     <Grid Tap="Tap_Handler"> 
      // .. your text or whatever goes here 
     </Grid> 
    </DataTemplate> 
</ListBox:ItemTemplate> 

和代码隐藏法宝:

public void Tap_Handler(object sender, GestureEventArgs e) 
{ 
    var item = (sender as Grid).DataContext as City; // Given you have City objects in your list 
    NavigationService.Navigate(new Uri("/View/City.xaml?id=" + item.Id, UriKind.Relative); 
} 

应该是类似的东西,如果没有正确的开箱工作。

+0

非常感谢。有用!!只有一些改变。使用NavigationService.Navigate代替NavigationContext.Navigate。编辑它。 – user1538895 2012-07-24 11:37:14

+0

但我怎样才能使用它导航到不同的页面。例如,我有City1.xaml City2.xaml和City3.xaml。当用户在我的列表选择器 – user1538895 2012-07-24 11:49:39

+0

@ user1538895中分别显示City1,City2和City3时,我该如何导航到这些页面,这听起来像是一个糟糕的解决方案,但如果你坚持这样做,你基本上只需插入Id或任何你有不同的位置如下所示:“/ View/City”+ item.Id +“.xaml”。 – 2012-07-24 11:51:47

1

您可以订阅活动LongListSelector

longListSelector.SelectionChanged += new SelectionChangedEventHandler(longListSelector_SelectionChanged); 
+0

我没有得到你 – user1538895 2012-07-24 11:47:23