2

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?

1
  • 1
    Is this ASP .NET or WinForms?
    – OwenP
    Commented Oct 8, 2008 at 20:50

6 Answers 6

4

ListBox will keep it's SelectedIndex regardless of focus.

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

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

2
  • TextBox has AutoPostBack = true set perhaps?
    – wprl
    Commented Oct 8, 2008 at 20:00
  • Perhaps but the tags indicate this is a C# .NET and my understanding is that there is no AutoPostBack property available in C# .NET for the TextBox. Commented Oct 8, 2008 at 20:24
3

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)

3

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.

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

<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

1
  • I had a very similar issue using WPF and MVVM pattern where I have a waterfall GUI, multiple listboxes next to eachother. And as soon as one of the listboxes lost their focus their selection was lost as well. Removing the Style Trigger you pointed out, fixed it for me. Thank you!
    – Zerratar
    Commented Apr 6, 2016 at 7:28
2

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

1

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.

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....

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

1

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.