wpf 在 C# 中取消选择列表框项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19116500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deselect listbox item in c#
提问by bytestorm
I am using listboxes in a windows phone application code written in c#.
我在用 c# 编写的 Windows 手机应用程序代码中使用列表框。
<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.SelectionChangedto navigate to some other page depending on the selection made by the user. Now when I navigate back to the page again I see the listITem still selected. How can I deselect that item? I tried to use listBox1.SelectedIndex = -1. But that seemed to call up the selectionChanged event handler.
我正在使用事件listBox1.SelectionChanged处理程序导航到其他页面,具体取决于用户所做的选择。现在,当我再次导航回页面时,我看到 listITEm 仍处于选中状态。如何取消选择该项目?我尝试使用listBox1.SelectedIndex = -1. 但这似乎调用了 selectionChanged 事件处理程序。
回答by
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 SelectionChangedevent handler to see if index is -1 (in that case don't execute the code). Hope this helps
但是在第二种情况下,您必须在SelectionChanged事件处理程序中放置一个断点以查看 index 是否为 -1(在这种情况下不要执行代码)。希望这可以帮助
回答by Anirudha
回答by Toni Petrina
Override OnNavigatedToand set nullas selected item.
覆盖OnNavigatedTo并设置null为选定项。

