取消选择c#中的列表框项

byt*_*orm 3 c# wpf listbox windows-phone-7

我在用c#编写的windows phone应用程序代码中使用列表框.

<Grid>
<ListBox x:Name ="gsecList" ItemsSource="{Binding}" SelectionChanged="ShowGsecDetails">
Run Code Online (Sandbox Code Playgroud)

事件处理程序:

private void ShowGsecDetails(object sender, SelectionChangedEventArgs e)
{
    string indexCode = gsecList.SelectedIndex.ToString();
    NavigationService.Navigate(new Uri("/contactDetail.xaml?type=gsec&index="+indexCode, UriKind.Relative));
}
Run Code Online (Sandbox Code Playgroud)

我使用eventhandler listBox1.SelectionChanged导航到其他页面,具体取决于用户所做的选择.现在当我再次导航回页面时,我看到listITem仍然被选中.我该如何取消选择该项目?我试着用listBox1.SelectedIndex = -1.但这似乎调用了selectionChanged事件处理程序.

小智 6

你可以做ListBox1.UnselectAll()ListBox1.SelectedIndex = -1

但在第二种情况下,您必须在SelectionChanged事件处理程序中放置一个断点,以查看index是否为-1(在这种情况下不执行代码).希望这可以帮助


Ani*_*dha 5

您可以调用UnselectAll()方法

listBox1.UnselectAll();
Run Code Online (Sandbox Code Playgroud)