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.
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:
ListViewItem item = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
item.Focus();
It's also possible to call
Keyboard.Focus(item);
If you also want to scroll the ListView
to the item's position, add this:
myListView.ScrollIntoView(item);
IMPORTANT NOTE: For this to work, you will need to set VirtualizingStackPanel.IsVirtualizing="False"
on your ListView
, which may cause it to perform slower. The reason this attached property is required is that when the ListView
is virtualized (which it is by default), the ListViewItems
aren't created for items that aren't displayed on the screen, which will cause ContainerFromIndex()
to return null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With