简体   繁体   English

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

[英]Deselect listbox item in c#

I am using listboxes in a windows phone application code written in c#. 我在用c#编写的windows phone应用程序代码中使用列表框。

<Grid>
<ListBox x:Name ="gsecList" ItemsSource="{Binding}" SelectionChanged="ShowGsecDetails">

Event Handler : 事件处理程序:

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

I am using the eventhandler listBox1.SelectionChanged to navigate to some other page depending on the selection made by the user. 我正在使用eventhandler listBox1.SelectionChanged导航到其他页面,具体取决于用户所做的选择。 Now when I navigate back to the page again I see the listITem still selected. 现在当我再次导航回页面时,我看到listITem仍然被选中。 How can I deselect that item? 我该如何取消选择该项目? I tried to use listBox1.SelectedIndex = -1 . 我试着使用listBox1.SelectedIndex = -1 But that seemed to call up the selectionChanged event handler. 但这似乎调用了selectionChanged事件处理程序。

You can either do ListBox1.UnselectAll() or ListBox1.SelectedIndex = -1 您可以执行ListBox1.UnselectAll()ListBox1.SelectedIndex = -1

But in the second case you have to put a breakpoint in the SelectionChanged event handler to see if index is -1 (in that case don't execute the code). 但在第二种情况下,您必须在SelectionChanged事件处理程序中放置一个断点,以查看index是否为-1(在这种情况下不执行代码)。 Hope this helps 希望这可以帮助

您可以调用UnselectAll()方法

listBox1.UnselectAll();

覆盖OnNavigatedTo并将null设置为所选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM