Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:57+00:00 2026-05-23T09:03:57+00:00

I have the following XAML: <ListBox HorizontalAlignment=Left Margin=0,6,0,10 Name=listBox1 Width=468 ItemsSource={Binding} Grid.ColumnSpan=1> <ItemsControl.ItemTemplate> <DataTemplate>

  • 0

I have the following XAML:

 <ListBox HorizontalAlignment="Left" Margin="0,6,0,10" Name="listBox1" Width="468" ItemsSource="{Binding}" Grid.ColumnSpan="1">
  <ItemsControl.ItemTemplate>
   <DataTemplate>
    <StackPanel Orientation="Horizontal">
     <TextBox Name="txt1" Text="{Binding Propty1}" IsEnabled="False"></TextBox>
     <CheckBox IsChecked="{Binding Propty2}" Name="{Binding Propty2}" Click="chk_Clicked"></CheckBox>
     <TextBox Text="{Binding Propty2}" Name="{Binding Propty3}" GotFocus="txt_GotFocus" LostFocus="txt_OnLostFocus" KeyDown="txt_OnKeyDown"/>
   </StackPanel>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ListBox>

After an event happens (a button outside of listbox is clicked, then the data in the listbox is reloaded), I need to focus on one of the textboxes in the listbox. I use VisualTreeHelper to go through the listbox. Here is the code:

    void SetFocusOnTextBox(DependencyObject element)
    {
        ListBoxItem listItem = element as ListBoxItem;
        if (listItem != null)
        {
            //find textbox and set focus here
        }

        int children = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < children; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(element, i);
            SetFocusOnTextBox(child);
        }
    }

However, I don’t get any items of type listbox when I call the method in the following line of code:

           SetFocusOnTextBox(listBox1);

I get ListBox, ScrollViewer, Border, Grid, ContentPresenter, ItemsPresenter, VirtualizingStackPanel and so on, but no listboxitems. What am I doing wrong? How do I find listboxitems (and then textboxes) in the listbox? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:03:58+00:00Added an answer on May 23, 2026 at 9:03 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XAML: ... <ListBox Name =RoomsListBox Height=100 HorizontalAlignment=Left Margin=12,41,0,0 VerticalAlignment=Top Width=120></ListBox>
I have a user control with the following XAML: <ScrollViewer> <ListBox ItemsSource={Binding Items}> <ListBox.ItemTemplate>
I have the following XAML code: <ListBox ItemsSource={Binding Languages}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source={Binding
I have the following XAML: <TextBlock Text={Binding ElementName=EditListBox, Path=SelectedItems.Count} Margin=0,0,5,0/> <TextBlock Text=items selected> <TextBlock.Style>
I have the following Xaml in a Window ( ArtistInfo ): <Grid> <TextBlock Text={Binding
How to get scroll up/down or left/right event for scrollViewer/Listbox? I have following XAML.
I have the following XAML: <ListBox SelectedItem={Binding SelectedTeam}> <ListBoxItem Content={Binding Match.HomeTeam} /> <ListBoxItem Content={Binding
I have the following Xaml (Silverlight, but it shouldn't matter): <ListBox x:Name=Results> ... </ListBox>
I'm developing a Windows Phone app. I have the following XAML code: <Grid x:Name=ContentPanel
I have a databound ListBox with an ItemTemplate, following this example : <ListBox ItemsSource={Binding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.