wpf ListBox.SelectedIndex = -1 不会取消设置所选项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15277033/
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 07:56:57  来源:igfitidea点击:

ListBox.SelectedIndex = -1 is not unsetting the selected item

c#wpflistboxselecteditem

提问by Harrison

I have a listbox defined in XAML is follows:

我有一个在 XAML 中定义的列表框如下:

<ListBox x:Name="listLeagues" DisplayMemberPath="LeagueName" 
         SelectionChanged="listLeagues_SelectionChanged"
         SelectedItem="{Binding SelectedObject, Mode=TwoWay}"
         SelectionMode="Single"    
         BorderBrush="{StaticResource noBrush}" Background="{StaticResource noBrush}"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled">
</ListBox>

In code I set the DataContext as follows: listLeagues.ItemsSource = Leagues;where 'Leagues' is a custom collection. I am trying to change the selected Item in code with the following code:

在代码中,我将 DataContext 设置如下: listLeagues.ItemsSource = Leagues;其中 'Leagues' 是自定义集合。我正在尝试使用以下代码更改代码中的选定项目:

    private void addButton_Click(object sender, MouseButtonEventArgs e)
    {
        FantasyLeague addedLeague = new FantasyLeague();
        Random rnd = new Random();
        addedLeague.LeagueName = "New Leauge " + rnd.Next(100).ToString();

        Leagues.Add(addedLeague);

        listLeagues.SelectedIndex = -1;
        listLeagues.SelectedItem = addedLeague;
        LeagueDetailsClickHelper();
    }

My listbox has SelectionMode = Single. The line listLeagues.SelectedIndex = -1;has no affect and when changing the SelectedItem to a new value it has both items as SelectedItems even though the mode is single. Can someone explain what is going wrong?

我的列表框具有 SelectionMode = Single。该行listLeagues.SelectedIndex = -1;没有影响,当将 SelectedItem 更改为新值时,即使模式是单一的,它也会将两个项目作为 SelectedItems。有人可以解释一下出了什么问题吗?

Thanks so much for your help.

非常感谢你的帮助。

EDIT

编辑

I have removed the SelectedItem="{Binding SelectedObject, Mode=TwoWay}"from the XAML and found that things are working properly [most of the time]. I am updating the underlying source of the ListBox and when changing values that affect the == operator assessment to be different from the original values then I have noticed the SelectionChanged event doesn't include anything in the RemovedItems. I am going to attempt to make a deep copy of the object and change the deep copy and see if that solves my issue.

我已经SelectedItem="{Binding SelectedObject, Mode=TwoWay}"从 XAML 中删除了,发现一切正常[大部分时间]。我正在更新 ListBox 的基础源,当将影响 == 运算符评估的值更改为与原始值不同时,我注意到 SelectionChanged 事件不包含 RemovedItems 中的任何内容。我将尝试制作对象的深层副本并更改深层副本,看看是否能解决我的问题。

FINAL UPDATE

最后更新

I made a deep copy of the FantasyLeague that is in the Leagues collection. I have members of the FantasyLeauge bound to a form which can be changed. When I made the deep copy of this the source of the form that allowed changes to the Leagues collection instead of the original there were no problems. I'm not sure why changing values that are linked to the assessment of == and .Equals would cause this behavior and not just update the target/source correctly, however, I have worked around the issue. If anyone has input on what causes this behavior I would appreciate it. Thanks for everyone that made suggestions.

我制作了 Leagues 收藏中的 FantasyLeague 的深层副本。我有一个 FantasyLeauge 成员绑定到一个可以改变的形式。当我将这个深层副本作为允许更改 Leagues 集合而不是原始集合的表单的源时,没有任何问题。我不确定为什么更改与 == 和 .Equals 评估相关的值会导致这种行为,而不仅仅是正确更新目标/源,但是,我已经解决了这个问题。如果有人对导致这种行为的原因有意见,我将不胜感激。感谢所有提出建议的人。

回答by Anthony Russell

Okay well first thing is you should be using an ObservableCollectionfor binding.

好的,首先你应该使用ObservableCollectionfor 绑定。

Second, you are setting the actual UI SelectedIndexto -1 (which completely defeats the purpose of binding). You need to set the index of the DataSource.

其次,您将实际 UI 设置SelectedIndex为 -1(这完全违背了绑定的目的)。您需要设置DataSource.

Try that and possibly post the rest of your code.

尝试一下,并可能发布其余的代码。

回答by yo chauhan

listLeagues.SelectedIndex = -1;
    listLeagues.SelectedItem = addedLeague;
**listLeagues.UpdateLayOut();**

I hope this will help.

我希望这将有所帮助。

回答by Ben H

I realize this is old but since it shows high in my search I wanted to update the answer. I think you should use .ClearSelected() method, MSDN info. That should clear out the any selections in a listbox C# or VBA (.net).

我意识到这是旧的,但由于它在我的搜索中显示很高,我想更新答案。我认为你应该使用 .ClearSelected() 方法,MSDN info。这应该清除列表框 C# 或 VBA (.net) 中的任何选择。

Cheers,

干杯,