Windows phone 7 列表框项目单击事件?
我将一些项目绑定到listbox。每当单击特定的listitem时,我都希望在另一个页面中显示该子项目 请告诉我怎么做Windows phone 7 列表框项目单击事件?,windows-phone-7,Windows Phone 7,我将一些项目绑定到listbox。每当单击特定的listitem时,我都希望在另一个页面中显示该子项目 请告诉我怎么做 如何在单击特定列表项时获取列表项id?查看作为新数据绑定应用程序一部分创建的代码。它就是这样做的。为了澄清(如果您不想创建一个新的数据绑定应用程序,只想得到答案),您应该查看列表框本身的selectionchanged事件,而不是依赖于列表框项目的click事件 例如: //列表框上的句柄选择已更改 private void Main ListBox_SelectionCha
如何在单击特定列表项时获取列表项id?查看作为新数据绑定应用程序一部分创建的代码。它就是这样做的。为了澄清(如果您不想创建一个新的数据绑定应用程序,只想得到答案),您应该查看列表框本身的selectionchanged事件,而不是依赖于列表框项目的click事件 例如:
//列表框上的句柄选择已更改
private void Main ListBox_SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
//如果所选索引为-1(无选择),则不执行任何操作
如果(MainListBox.SelectedIndex==-1)
返回;
//导航到新页面
NavigationService.Navigate(新Uri(“/DetailsPage.xaml?selectedItem=“+MainListBox.SelectedIndex,UriKind.Relative”);
//将所选索引重置为-1(无选择)
MainListBox.SelectedIndex=-1;
}
MVVM解决方案
使用以下库:
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" <-- (optional, only for the ContextMenu)
xmlns:GalaSoft\u MvvmLight\u Command=“clr命名空间:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71”
xmlns:toolkit=“clr namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.toolkit”对于将MVVM样式绑定到列表中某个项的简单命令,您还可以将整个项模板包含在按钮中,并连接按钮的command属性,如下所示:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Command="{Binding Path=SomeCommandOnItemViewModel, Mode=OneWay}">
<Button.Template>
<ControlTemplate>
<!-- Your listitem styling goes here -->
</ControlTemplate>
</Button.Template>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
@Blakomen:还有什么条件
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="categoryListBox" Margin="0,0,-12,0" ItemsSource="{Binding CategoryItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stackPanel" Margin="0,0,0,17" Width="432" Height="78">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding Main.OpenCategoryCommand, Source={StaticResource Locator}, Mode=OneWay}"
CommandParameter="{Binding Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Remove" />
<toolkit:MenuItem Header="Show" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Note}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Command="{Binding Path=SomeCommandOnItemViewModel, Mode=OneWay}">
<Button.Template>
<ControlTemplate>
<!-- Your listitem styling goes here -->
</ControlTemplate>
</Button.Template>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>