在列表框上使用 VisualTreeHelper,无法获取列表框项 [英] Using VisualTreeHelper on listbox, can't get listboxitems

查看:21
本文介绍了在列表框上使用 VisualTreeHelper,无法获取列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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>

事件发生后(单击列表框外的按钮,然后重新加载列表框中的数据),我需要关注列表框中的文本框之一.我使用 VisualTreeHelper 来浏览列表框.代码如下:

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

我得到了 ListBox、ScrollViewer、Border、Grid、ContentPresenter、ItemsPresenter、VirtualizingStackPanel 等等,但没有 listboxitems.我究竟做错了什么?如何在列表框中找到列表框项目(然后是文本框)?谢谢.

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.

推荐答案

Silverlight 是异步的!

...重新加载列表框中的数据...

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

当您使用一些新数据更改 DataContext 时,并不意味着将立即加载实际数据.就在您尝试获取 ListboxItem 之前,只需调用 listBox1.UpdateLayout ,一切都应该没问题.

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

这篇关于在列表框上使用 VisualTreeHelper,无法获取列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆