简体   繁体   English

失去焦点后,列表框保持选择

[英]ListBox keep selection after losing focus

I have a ListBox that when in focus, and when I have an item selected returns a valid SelectedIndex. 我有一个ListBox,当焦点对准时,当我选择一个项目时,它将返回一个有效的SelectedIndex。 If I have a valid SelectedIndex and I click on a TextBox on the same Forum, the SelectedIndex now becomes -1. 如果我有一个有效的SelectedIndex并在同一论坛上单击一个TextBox,则SelectedIndex现在变为-1。 However I want it to keep its SelectedIndex from changing. 但是我希望它保持其SelectedIndex不变。 How would I go about doing this? 我将如何去做呢?

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. 我在一个空白项目中对其进行了测试,该项目具有一个ListBox,一个TextBox和一个用于显示ListBox的SelectedIndex的Label。 Under both the ListBox's SelectedIndexChanged and the TextBox's TextChanged events I updated the Label with the ListBox's SelectedIndex 在列表框的SelectedIndexChanged和文本框的TextChanged事件下,我使用列表框的SelectedIndex更新了Label

There must be something else going on to cause the Selected Index to change to -1. 必须进行其他操作才能使“选定索引”更改为-1。

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,问题应该消失了

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(并且可以还原它,则将其重置)就将其重置

处理SelectedIndexChanged事件并保存选定的值,以便在控件重新获得焦点时可以将其还原。

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. 我尚未在我的应用程序中对此进行验证,但是如果LB失去焦点时,如果SelectedIndex属性发生了变化,则您可能必须自己处理这种情况,方法是缓存最后一个选定的索引,并在控件重新获得焦点时将其重置。 You can do this in the containing form or you can do it in a class derived from ListBox. 您可以以包含表单的形式执行此操作,也可以在从ListBox派生的类中执行此操作。

You could even try setting the selected index as soon as you see it becomes -1. 您甚至可以在看到选定索引为-1时立即尝试设置它。 Not sure what would happen but I'd be curious to find out.... 不知道会发生什么,但我很想知道...。

Edit: just tested it and like the other poster I can't reproduce it either. 编辑:刚刚测试过,就像其他海报一样,我也无法复制。 Must be something slightly different about your LB 与您的LB一定有所不同

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. 这是我能想到的唯一方法,即在更改焦点时您将失去SelectedIndex。 Otherwise, how would anybody eg click a button to take action on an item? 否则,任何人(例如,单击按钮)如何对某项采取行动? You'd lose the selection when focus went to the button you're clicking... 当焦点移到您要单击的按钮上时,您将丢失选择...

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

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