is there any way to accomplish this functionality from WinForms in WPF?
ListView.FocusedItem = ListView.Items[itemToFocusIndex]
I'm trying to manually set focus (not select) on item in WPF ListView. From System.Windows.Controls. Thanks.
is there any way to accomplish this functionality from WinForms in WPF?
ListView.FocusedItem = ListView.Items[itemToFocusIndex]
I'm trying to manually set focus (not select) on item in WPF ListView. From System.Windows.Controls. Thanks.
I believe you can use Keyboard.FocusedElement to get the focused element in the listview.
should return the focused element
ListView items are UIElements, so simply use
UIElement.Focus()
. e.g.,listViewItem.Focus()
orbutton.Focus()
and so on.There are two types of focus in WPF - Keyboard Focus and Logical Focus. This link can give you more information about focus in WPF.
You can either do this:
It's also possible to call
If you also want to scroll the
ListView
to the item's position, add this:IMPORTANT NOTE: For this to work, you will need to set
VirtualizingStackPanel.IsVirtualizing="False"
on yourListView
, which may cause it to perform slower. The reason this attached property is required is that when theListView
is virtualized (which it is by default), theListViewItems
aren't created for items that aren't displayed on the screen, which will causeContainerFromIndex()
to returnnull
.