WPF listview selected item/index problem

L

Lloyd Sheen

I have a strange WPF problem with a Listview. The listview contains a list
of media files. The first column of GridView is a button which when clicked
causes the media selected to play within the app.

Clicking the button will set the selected index/item of the listview
corrrectly (I have a label I am now using to debug and are showing the
listview.selectedindex).

If I populate the listview I can use the down arrow to navigate the listview
with the following problems:

1. The first down arrow press does nothing, the second is handled (I have
no code for handling the key strokes so this is WPF behaviour. This happens
each time the listview is populated. I set the listview.selecteditem to
nothing in the populate code.

2. If I click the button for one of the items and then click the down
arrow the selected item has been changed to -1 (I do not do this in code and
the populate (see above) is not called. In the button click I set the
selected item

Why does the selectedindex/item change without my/user guidance?

There is lots of code. If needed I will post.

Thanks
Lloyd Sheen
 
L

Lloyd Sheen

Patrice said:
So the item you set in the button click doesn't exists (not sure where the
button takes the item to select) ? Then you would have -1 to indicate that
nothing is selected...

We don't need the whole code. You need to create the simplest "proof of
concept" that shows this problem. Or quickly built a simple app that just
define the selected item from a button click, each item being whatever
dummy data that may fit.

During this simplification process you'll likely find yourself the
cullprit. If not you have then the minimal amount of code that shows the
problem so that others can give a look at it..

This is a trace with the simplest of code: the events I did are surrounded
by **********


****************
Initial load of Listview
****************

Focus got by : Button CDButton selected index is -1
Focus got by : ListViewItem selected index is -1
Focus got by : ScrollViewer selected index is -1
Focus got by : ListView CDList selected index is -1
Focus got by : FolderList CDList selected index is -1
Focus got by : FolderNavigator theFolderNavigator selected index is -1
Focus got by : TabItem selected index is -1
Focus got by : TabControl navTabs selected index is -1
Focus got by : MainWindow selected index is -1
Focus lost by : Button CDButton selected index is -1
Focus lost by : ListViewItem selected index is -1
Focus lost by : ScrollViewer selected index is -1
Focus lost by : ListView CDList selected index is -1
Focus lost by : FolderList CDList selected index is -1
Focus lost by : FolderNavigator theFolderNavigator selected index is -1
Focus lost by : TabItem selected index is -1
Focus lost by : TabControl navTabs selected index is -1
Focus lost by : MainWindow selected index is -1
Focus got by : ListView SongView selected index is -1
SongView_GotFocus

SelectedIndexChange 0 - from the SelectedIndexChange Event


**********
Pressed DownArrow
**********
Focus lost by : ListView SongView selected index is 0
SongView_LostFocus

Focus got by : ListViewItem selected index is 0
Focus got by : ScrollViewer selected index is 0
Focus got by : ListView SongView selected index is 0
SongView_GotFocus

Key up Down selected index = 0

**********
Pressed DownArrow
**********

Focus lost by : ListViewItem selected index is 0
Focus lost by : ScrollViewer selected index is 0
Focus lost by : ListView SongView selected index is 0
SongView_LostFocus

Focus got by : ListViewItem selected index is 0
Focus got by : ScrollViewer selected index is 0
Focus got by : ListView SongView selected index is 0
SongView_GotFocus

SelectedIndexChange 1 - from the SelectedIndexChange Event

Key up Down selected index = 1

**************************
And Again pressed a DownArrow
**************************

Focus lost by : ListViewItem selected index is 1
Focus lost by : ScrollViewer selected index is 1
Focus lost by : ListView SongView selected index is 1
SongView_LostFocus

Focus got by : ListViewItem selected index is 1
Focus got by : ScrollViewer selected index is 1
Focus got by : ListView SongView selected index is 1
SongView_GotFocus

SelectedIndexChange 2 - from the SelectedIndexChange Event

Key up Down selected index = 2

********************
Pressed Keypress - Space
********************

Key up Space selected index = 2

Focus lost by : ListViewItem selected index is 2
Focus got by : ListView SongView selected index is 2
SongView_GotFocus


**********
Pressed DownArrow
**********

Focus lost by : ListView SongView selected index is 2
SongView_LostFocus

Focus got by : ListViewItem selected index is 2
Focus got by : ScrollViewer selected index is 2
Focus got by : ListView SongView selected index is 2
SongView_GotFocus

SelectedIndexChange 0 - from the SelectedIndexChange Event

###############################################
This is where it screws up. Notice that the index was 2 and that
the SongView (the listview) got focus and the selected index has changed.
Of course it the call stack for this shows only an entry for "external code"
which is where the screwup occurs. I have been trying to fix this one
thing for about 5 days. I have read so many internet entries about WPF
problems I am just about to give up on this tech as useless.
###################################################

Key up Down selected index = 0

Focus got by : Button CDButton selected index is -1
Focus got by : ListViewItem selected index is -1
Focus got by : ScrollViewer selected index is -1
Focus got by : ListView CDList selected index is -1
Focus got by : FolderList CDList selected index is -1
Focus got by : FolderNavigator theFolderNavigator selected index is -1
Focus got by : TabItem selected index is -1
Focus got by : TabControl navTabs selected index is -1
Focus got by : MainWindow selected index is -1
Focus lost by : Button CDButton selected index is -1
Focus lost by : ListViewItem selected index is -1
Focus lost by : ScrollViewer selected index is -1
Focus lost by : ListView CDList selected index is -1
Focus lost by : FolderList CDList selected index is -1
Focus lost by : FolderNavigator theFolderNavigator selected index is -1
Focus lost by : TabItem selected index is -1
Focus lost by : TabControl navTabs selected index is -1
Focus lost by : MainWindow selected index is -1
Focus got by : ListView SongView selected index is -1
SongView_GotFocus

SelectedIndexChange 0

********************
Pressed DownArrow
********************
Focus lost by : ListView SongView selected index is 0
SongView_LostFocus

Focus got by : ListViewItem selected index is 0
Focus got by : ScrollViewer selected index is 0
Focus got by : ListView SongView selected index is 0
SongView_GotFocus

Key up Down selected index = 0

*****************
Pressed DownArrow
*****************

Focus lost by : ListViewItem selected index is 0
Focus lost by : ScrollViewer selected index is 0
Focus lost by : ListView SongView selected index is 0
SongView_LostFocus

Focus got by : ListViewItem selected index is 0
Focus got by : ScrollViewer selected index is 0
Focus got by : ListView SongView selected index is 0
SongView_GotFocus


SelectedIndexChange 1
Key up Down selected index = 1

******************
And Again
******************

Focus lost by : ListViewItem selected index is 1
Focus lost by : ScrollViewer selected index is 1
Focus lost by : ListView SongView selected index is 1
SongView_LostFocus

Focus got by : ListViewItem selected index is 1
Focus got by : ScrollViewer selected index is 1
Focus got by : ListView SongView selected index is 1
SongView_GotFocus

SelectedIndexChange 2

Key up Down selected index = 2

******************
Keypress - Space
******************

Key up Space selected index = 2

Focus lost by : ListViewItem selected index is 2
Focus got by : ListView SongView selected index is 2
SongView_GotFocus

DownArrow

Focus lost by : ListView SongView selected index is 2
SongView_LostFocus

Focus got by : ListViewItem selected index is 2
Focus got by : ScrollViewer selected index is 2
Focus got by : ListView SongView selected index is 2
SongView_GotFocus

SelectedIndexChange 0

Key up Down selected index = 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top