Using VisualTreeHelper on listbox, can't get listboxitems

后端 未结 2 870
小鲜肉
小鲜肉 2020-12-21 08:07

I have the following XAML:

 

        
2条回答
  •  Happy的楠姐
    2020-12-21 08:57

    Please try this code snipped below to get the TextBox in ListBoxItem.

    ListBox listbox = element as ListBox;
    if (listbox != null && listbox.SelectedItem !=null)
    {             
    //find textbox and set focus here 
    
    Textbox thisTextBox = (listbox.SelectedItem).Find("txt1") as Textbox;
    if(thisTextBox !=null)
    {
        thisTextBox.Focus();
        return;
    }
    
    }
    int children = VisualTreeHelper.GetChildrenCount(element);         
    for (int i = 0; i < children; i++)         
    {
         DependencyObject child = VisualTreeHelper.GetChild(element, i);
         SetFocusOnTextBox(child);         
    } 
    

提交回复
热议问题