Using VisualTreeHelper on listbox, can't get listboxitems

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

I have the following XAML:

 

        
相关标签:
2条回答
  • 2020-12-21 08:45

    Silverlight is Asynchronous!

    ...the data in the listbox is reloaded...

    When you change DataContext with some new data it doesn't mean that actual data will be loaded immediately. Just before you trying to get ListboxItem just simply call listBox1.UpdateLayout and everything should be fine.

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
    
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
    
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ReloadData();
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // 1 - Uncomment this line to crush your app
            // ReloadData();
    
            // 2 - Uncomment this line to fix everything
            // listBox1.UpdateLayout();
    
            // UpdateLayout - ensures that actual data is loaded to UI,             
            // all items are created and rendered 
    
            GetItemsRecursive(listBox1);
        }
    
        private void GetItemsRecursive(DependencyObject lb)
        {
            var childrenCount = VisualTreeHelper.GetChildrenCount(lb);
    
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(lb, i);
    
    
                if (child is ListBoxItem)
                {
                    MessageBox.Show(child.GetType().ToString());
                    return;
                }
    
                GetItemsRecursive(child);
            }
        }
    
        private void ReloadData()
        {
            listBox1.DataContext = new List<Classs>() {
                new Classs{ Propty1 = "sasda", Propty2 = false, Propty3 = "asdasda"},
                new Classs{ Propty1 = "sasda", Propty2 = true, Propty3 = "asdasda"},
                new Classs{ Propty1 = "sasda", Propty2 = false, Propty3 = "asdasda"}
            };
        }
    }
    
    public class Classs
    {
        public string Propty1 { get; set; }
    
        public bool Propty2 { get; set; }
    
        public string Propty3 { get; set; }
    }
    
    0 讨论(0)
  • 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);         
    } 
    
    0 讨论(0)
提交回复
热议问题