Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

silverlight - Using VisualTreeHelper on listbox, can't get listboxitems

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...