Set Item Focus in ListView WPF

后端 未结 5 1810
轻奢々
轻奢々 2020-11-29 10:45

is there any way to accomplish this functionality from WinForms in WPF?

ListView.FocusedItem = ListView.Items[itemToFocusIndex]

I\'m trying

相关标签:
5条回答
  • 2020-11-29 11:08
    //to set focus write
    CollistView7.Items[TheIndItem].Selected = true; 
    CollistView7.Select();
    CollistView7.Items[TheIndItem].Focused = true;
    //when TheIndItem is the index
    
    0 讨论(0)
  • 2020-11-29 11:11

    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.

    0 讨论(0)
  • 2020-11-29 11:11

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

    Keyboard.FocusedElement
    

    should return the focused element

    0 讨论(0)
  • 2020-11-29 11:18

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

    0 讨论(0)
  • 2020-11-29 11:18
        public void foucusItem( ListView.Item itemToFocusIndex){
             int count = 0; 
             foreach(ListView.Item item in YourListView){
                   if(item == itemsToFocusIndex){
                         ListView.Items[count].Focus();
                         return;
                   }
             count++;
             }
        }
    
    0 讨论(0)
提交回复
热议问题