WP7 - VisualTreeHelper 循环遍历所有列表框项目 [英] WP7 - VisualTreeHelper to loop through all ListBox Items

查看:23
本文介绍了WP7 - VisualTreeHelper 循环遍历所有列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据选择(选中)的项目创建一个新的 ListBox.如果 ListBox 上只有 20 个项目,以下代码实际上有效,但添加更多项目会使其崩溃.任何人都可以知道如何使它工作,或者有不同的方法吗?循环遍历列表框是否有限制?

I need to create a new ListBox based on items that are selected (checked). The following code actually worked if I only had like 20 items on the ListBox, but adding more items make it crash. Can anybody know how to make it work, or have a different aproach? Is there a limite for looping through a listBox?

    // worked fine for 20 items,
    // but my actual list contems 95 items...
    private void btnCreateNewList_Click(object sender, RoutedEventArgs e)
    {

                int totalItemsCB = ListCheckBoxVocabulary.Items.Count;
                for (int ii = 0; ii < totalItemsCB-1; ii++)
                {
                    ListBoxItem item = this.ListCheckBoxVocabulary.ItemContainerGenerator.ContainerFromIndex(ii) as ListBoxItem;
                    CheckBox thisCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
                    if (thisCheckBox.IsChecked == true) 
                    {

                        dataPlayListSource.Add(new SampleData() { Text = thisCheckBox.Content.ToString() + " | " + ii });
                        // this.PlayListCheckBoxVocabulary.UpdateLayout();
                        this.PlayListCheckBoxVocabulary.ItemsSource = dataPlayListSource;
                    }

                }
    }

    private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T)
            {
                return (T)child;
            }
            else
            {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }

和xaml:

        <controls:PivotItem Header="Vocabulary" >
            <ListBox x:Name="ListCheckBoxVocabulary" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--<StackPanel Margin="0,0,0,17" Width="432">-->
                        <CheckBox x:Name="cbVocabulary" Content="{Binding Text}" Checked="CheckBox_Checked" Unchecked="UncheckBox" />
                        <!--</StackPanel>-->
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PivotItem>

推荐答案

该列表是虚拟的 - 控件在需要时创建并可能重用(我认为).

The list is virtual - controls are created as they are needed and potentially reused (I think).

您的选择是将 ListBox 变为不虚拟化(覆盖模板,对于容器,而不是 SerializedStackPanel,选择常规的 StackPanel).

Your options are to turn the ListBox to not be virtualized (override the template, and for the container, instead of a SerializedStackPanel, choose a regular StackPanel).

您的另一个(也是更可取的)选择是通过数据绑定进行检查.在大多数情况下更容易、更快捷.

Your other (and preferable) option is to do the checking via Data Binding. Way easier and faster in most situations.

这篇关于WP7 - VisualTreeHelper 循环遍历所有列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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