Setting focus to first item in a listview when it's itemsource is refreshed

Sep 4, 2012 at 1:49 PM

Hi all,

I was wondering if it is possible to set the focus on a listview item when this listview's itemsource is refreshed. I have been looking at the demo application but that uses the TextBoxFocusBehaviour. And i was wondering if there is some functionality like this to use on Listviews.

My case is:
I have a view that has an input box for searching parameters, a button to start the search and a listview to hold the results. The parameters inputbox has a binding to a property on my viewmodel. the Search button uses a command on my viewmodel and the listview binds it's ItemSource to an IcollectionView property on my viewmodel.
What I want to accomplish is that when search is selected and the viewmodel has the results and updates the IcollectionView property. that the first item of the Listview has the focus. so i can use the keyboard to navigate the list and use enter to select it.   

I would love to hear some idea's how to achieve this behaviour!
Kind regards,
Ruud

Sep 11, 2012 at 8:34 AM

Yeah there is nothing like that in Cinch. What you would need to do is either

a) Create Blend Behaviour or regular attached property, which is applied to ListView, and hook into ListView.DataContextChanged event, grab the datasource in the event handler and possible move the selection to the ListBoxItem

 item.BringIntoView();

 

b) Go for ALL ViewModel approach and wrap your DataSource in ICollectionView and move the CurrentItem. Have a look here : http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/

 

That is the best advise I can offer