I have an ItemControl in my view related to an ObservableCollection from ViewModel. The collection is populated, and after that there is an event from VM for viewing (I think, the search results and the SearchFinished event).
I would like to move the keyboard focus to the first element in the ItemsControl, but when I do this in the "Code Encoding" view while processing SearchFinished, the elements are not yet displayed (the collection is already full, but the wpf rendering is asynchronous and hasn't happened yet), so thereβs nothing to focus on (Focus () must have a visual element tree already built).
I wanted to do (myItemsControl.ItemContainerGenerator.ContainerFromIndex (0) as UIElement) .Focus (); but since the 0th item is not loaded yet, ContainerFromIndex (0) returns null.
I tried to defer it with Dispatcher.BeginInvoke ... with low priority, but it depends on the exact time and usually does not work.
How can I wait for the first item to load in the ItemsControl?
wpf focus loaded itemscontrol
TomΓ‘Ε‘ Kafka
source share