C# ListBox 失去焦点后保持选择

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

ListBox keep selection after losing focus

c#.netlistbox

提问by Redbaron

I have a ListBox that when in focus, and when I have an item selected returns a valid SelectedIndex. If I have a valid SelectedIndex and I click on a TextBox on the same Forum, the SelectedIndex now becomes -1. However I want it to keep its SelectedIndex from changing. How would I go about doing this?

我有一个 ListBox,当它处于焦点时,当我选择了一个项目时,它会返回一个有效的 SelectedIndex。如果我有一个有效的 SelectedIndex 并且我在同一个论坛上单击一个 TextBox,则 SelectedIndex 现在变为 -1。但是我希望它保持其 SelectedIndex 不变。我该怎么做呢?

采纳答案by PersistenceOfVision

ListBox will keep it's SelectedIndex regardless of focus.

无论焦点如何,ListBox 都将保持它的 SelectedIndex。

I tested it on a blank project with one ListBox, one TextBox and one Label used to display the ListBox's SelectedIndex. Under both the ListBox's SelectedIndexChanged and the TextBox's TextChanged events I updated the Label with the ListBox's SelectedIndex

我在一个空白项目中对其进行了测试,其中包含一个 ListBox、一个 TextBox 和一个用于显示 ListBox 的 SelectedIndex 的标签。在 ListBox 的 SelectedIndexChanged 和 TextBox 的 TextChanged 事件下,我用 ListBox 的 SelectedIndex 更新了标签

There must be something else going on to cause the Selected Index to change to -1.

必须有其他原因导致 Selected Index 更改为 -1。

回答by jeffm

Handle the SelectedIndexChangedevent and save the selected value so that you can restore it when your control regains focus.

处理SelectedIndexChanged事件并保存所选值,以便在控件重新获得焦点时可以恢复它。

回答by Andrew Queisser

I haven't verified this in my apps but if the SelectedIndex property changes when the LB loses focus you probably have to handle that case yourself by caching the last selected index and resetting it when the control regains focus. You can do this in the containing form or you can do it in a class derived from ListBox.

我还没有在我的应用程序中验证这一点,但是如果 SelectedIndex 属性在 LB 失去焦点时发生变化,您可能必须通过缓存最后选择的索引并在控件重新获得焦点时重置它来自己处理这种情况。您可以在包含表单中执行此操作,也可以在从 ListBox 派生的类中执行此操作。

You could even try setting the selected index as soon as you see it becomes -1. Not sure what would happen but I'd be curious to find out....

您甚至可以在看到所选索引变为 -1 时立即尝试设置它。不知道会发生什么,但我很想知道......

Edit: just tested it and like the other poster I can't reproduce it either. Must be something slightly different about your LB

编辑:刚刚测试过它,就像另一张海报一样,我也无法复制它。您的 LB 必须略有不同

回答by Coderer

Are these controls in different dialogs, or maybe different tabs on a tabbed container? That's the only way I can think of that you would lose your SelectedIndex when changing focus. Otherwise, how would anybody e.g. click a button to take action on an item? You'd lose the selection when focus went to the button you're clicking...

这些控件是在不同的对话框中,还是在选项卡式容器上的不同选项卡中?这是我能想到的唯一方法,即在更改焦点时会丢失 SelectedIndex。否则,有人怎么会例如单击按钮对项目采取行动?当焦点转到您单击的按钮时,您将失去选择...

回答by Dimestore Cowboy

I had the same issue as original poster. I couldn't figure it out totally but it seems like when you have the listbox bound to an observable collection and the collection gets changed that the selected item loses the focus.

我遇到了与原始海报相同的问题。我无法完全弄清楚,但似乎当您将列表框绑定到可观察集合并且集合发生更改时,所选项目失去了焦点。

I hacked around the issue by saving the selected index in a variable and resetting it if the selected index was -1 (and it was valid to restore it)

我通过将所选索引保存在一个变量中并在所选索引为 -1 时重置它来解决这个问题(并且恢复它是有效的)

回答by Danny S

This is an old question, but in case someone else experiences the same problem check your ListBoxItem style especially if you are using one of styles from WPF Themes.

这是一个老问题,但如果其他人遇到同样的问题,请检查您的 ListBoxItem 样式,特别是如果您使用的是 WPF Themes 中的样式之一

The problem with WPF Themes specifically is the inclusion of the section outside of the Control Template:

WPF 主题的问题特别在于包含了控制模板之外的部分:

<Style d:IsControlPart="True" TargetType="{x:Type ListBoxItem}">
....
<Style.Triggers>
        <Trigger Property="Selector.IsSelected" Value="True">
            <Setter Property="Foreground">
                <Setter.Value>
                    <SolidColorBrush po:Freeze="True" Color="{DynamicResource BlackColor}" />
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsKeyboardFocusWithin" Value="true">
            <Setter Property="IsSelected" Value="true" />
        </Trigger>
    </Style.Triggers>
</Style>

Delete the Style.Triggers and the problem should go away

删除 Style.Triggers,问题就会消失