WP7 - VisualTreeHelper to loop through all ListBox

2020-04-16 18:08发布

问题:

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.