tags:

views:

1915

answers:

2

Hi everyone!

I have some strange behavior of ListView control in WPF.

We are developing application in C#.

I created ListView with some items and set SelectedIndex to some index in the middle of its list. Then I clicked mouse at any place on the ListView and then navigate with Up/Dows keyboard keys, everything works perfectly. But If I move focus to another control and then tries to set focus back to ListView control programmatically, then I'm getting the problem.

The ListView gets the focus, but when I’m trying to navigate with the keyboard, current selection will be canceled and the first item will be selected and navigation will start from the first item in list.

I'm setting focus to ListView control by calling to its .Focus() method.

It looks like even ListView control in focus, but no items actually in focus, even if SelectedIndex has some valid values.

Please, help me! How can I set focus to ListView control programmatically to prevent such behavior?

i find out that my listview selected index is different from the focused item . who ???

in selectedindex_changed event i set the selected index to the fix item (5) , but when i press arrow keys the item was changed . that mean the selected index is fixed to 5 but the current item(shown with a dot border around it ) moved to the arrow key location . anyway my selecteditem is fix to 5 , but my scrollviews moves to position that current item is .

this code is in listView1_SelectionChanged: listView1.SelectionMode = SelectionMode.Single; listView1.SelectedIndex = 5;

my listview has a modified view :

    <l:PlainView x:Key="tileView"
            ItemTemplate="{StaticResource centralTile}"
            ItemHeight="120" ItemWidth="130" />

i dont know about the problom any more .

regards

+3  A: 

ListView probably suffers from the same issue that ListBox does, in that it can have focus independently of its items.

When I want to focus a ListBox, I need to do something like this:

listBox1.Focus();
if (listBox1.Items.Count > 0)
{
    var index = listBox1.SelectedIndex;
    if (index < 0) index = 0;

    var item = listBox1.ItemsContainerGenerator
                   .ContainerFromIndex(index) as ListBoxItem;
    item.Focus();
}

So this focuses the ListBox, but then if the ListBox contains any items it focuses either the selected item, or the first item if one is selected.

I've never used ListView in WPF, but presumably you could hack together a similar method using ListView instead of ListBox and [ListViewItem] in place of ListBoxItem.

Matt Hamilton
I would recommend setting Focusable=False on the ListBox/ListView, so that it can't obtain independent focus, and then just use:if (uiListBox.SelectedItem != null){(uiListBox.SelectedItem as UIElement).Focus();}
rmoore
Yeah, I've toyed with setting Focusable=False on the host control, but that does give you some weird focus problems when the list is empty. Sometimes you still want focus within an empty list so you can catch list-specific shortcut keys etc. Perhaps binding Focusable to Items.Count with a trigger would work better.
Matt Hamilton
ListView is a child of ListBox, so your example worked great for me. Only thing I changed is to set listBox1.SelectedIndex to 0, too, if (index < 0), so focus and selected item match.
Sam
@rmoore, uiListBox.SelectedItem might not even be an UIElement. At least it ain't one in my cases.
Sam
A: 

Isn't calling .Focus() generally a bad practice in any GUI/frameworks/languages ?

RocketSurgeon
So, how else do you set the focus then?
Sam