WP7 - VisualTreeHelper to loop through all ListBox Items

情到浓时终转凉″ 提交于 2019-12-31 02:43:12

问题


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

and 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>

回答1:


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

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.



来源:https://stackoverflow.com/questions/9897141/wp7-visualtreehelper-to-loop-through-all-listbox-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!