I am trying to implement a little search module in a WPF application. The results of the search are bound to a ListView and I want to set the focus on the first item of this ListView as soon as it is populated with new results.
The solution of this question - Set Item Focus in ListView WPF - suggests calling ContainerFromIndex(int index) method to get the necessary ListViewItem. Trying this at the very end of my search procedure returns null. So, I deduce that although the source of the ListView has been populated, the view itself isn't updated at this moment.
The next thing I tried is handling a SourceUpdated event of my ListView. It doesn't fire. I added NotifyOnSourceUpdated=True to the binding as was pointed out in the answer of this question - sourceupdated event not firing - but the event still doesn't fire.
I'll just add that the binding itself works. So, what is the right way to set focus on the first item (or on any other item) of a ListView?
EDIT: I'll supply the relevant part of the XAML to make things more clear:
<ListView Name="foundRequestsListView"
IsSynchronizedWithCurrentItem="True"
utils:GridViewSort.AutoSort="True"
ItemsSource="{Binding Path=FoundRequests, NotifyOnSourceUpdated=True}"
SourceUpdated="foundRequestsListView_SourceUpdated" >
<ListView.Resources>...</ListView.Resources>
<ListView.View>
<GridView>...</GridView>
</ListView.View>
</ListView>
FoundRequests property is an ordinary List.
EDIT: Calling UpdateLayout() beforehand solves the problem.