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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:45:50  来源:igfitidea点击:

Deselect listbox item in c#

c#wpfwindows-phone-7listbox

提问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

You can call UnselectAll()method

您可以调用UnselectAll()方法

listBox1.UnselectAll();

回答by Toni Petrina

Override OnNavigatedToand set nullas selected item.

覆盖OnNavigatedTo并设置null为选定项。