9

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.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
jnovacho
  • 2,825
  • 6
  • 27
  • 44

5 Answers5

26

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.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • Well I got the idea behind your code, but it's not working as _myListView.Items[itemIndex] as IInputElement;_ returns _null_. The items in the ListView are instances of my custom class and the proper style is selected at runtime according to instace property. The class does not inherit of implement any class or interface respectively; so I cannot cast it to IInputElement... – jnovacho Jul 31 '12 at 20:15
  • @jnovacho Any control inheriting from `UIElement` implements `IInputElement`. What type of object do you get from `myListView.Items[itemIndex]`? – Adi Lester Jul 31 '12 at 20:23
  • I get only Object from that Collection. – jnovacho Jul 31 '12 at 20:31
  • @jnovacho Look in the debugger and see what's the actual type that's being held there. – Adi Lester Jul 31 '12 at 20:32
  • So in the collection are actually stored instances of my custom class. Which only has two properties. Flag for choosing appropriate style, using DataTemplateSelector and second property which holds the actual data displayed in the UI. – jnovacho Jul 31 '12 at 20:48
  • Very important to check if the ListViewItem is null and call `ListView.UpdateLayout()`. Then calling the `...ContainerFromIndex` won't be null. – The Muffin Man Jun 30 '20 at 22:41
0

I believe you can use Keyboard.FocusedElement to get the focused element in the listview.

Keyboard.FocusedElement

should return the focused element

Josh
  • 10,352
  • 12
  • 58
  • 109
0

ListView items are UIElements, so simply use UIElement.Focus(). e.g., listViewItem.Focus() or button.Focus() and so on.

Ahmad
  • 1,462
  • 15
  • 23
-1
//to set focus write
CollistView7.Items[TheIndItem].Selected = true; 
CollistView7.Select();
CollistView7.Items[TheIndItem].Focused = true;
//when TheIndItem is the index
rony sc
  • 71
  • 2
-2
    public void foucusItem( ListView.Item itemToFocusIndex){
         int count = 0; 
         foreach(ListView.Item item in YourListView){
               if(item == itemsToFocusIndex){
                     ListView.Items[count].Focus();
                     return;
               }
         count++;
         }
    }
Zac
  • 2,201
  • 24
  • 48