Adrian
Adrian

Reputation: 20068

Cannot select item from a ListBox while generating them

So I am using C# with WPF. I have a list box where I generate 100 Music Stations, but I want to select one of them while there are generating, but I can't, only when they are done generating.

I am using this code to center the selected item (taken from msdn blog):

FrameworkElement container = listRadioItems.ItemContainerGenerator.ContainerFromItem(fixedItem) as FrameworkElement;

            if (null != container)
            {
                if (ScrollViewer.GetCanContentScroll(listBox))
                {
                    IScrollInfo scrollInfo = VisualTreeHelper.GetParent(container) as IScrollInfo;
                    if (null != scrollInfo)
                    {
                        StackPanel stackPanel = scrollInfo as StackPanel;
                        VirtualizingStackPanel virtualizingStackpanel = scrollInfo as VirtualizingStackPanel;

                        int index = listBox.ItemContainerGenerator.IndexFromContainer(container);

                        if (((null != stackPanel) && (Orientation.Horizontal == stackPanel.Orientation)) || ((null != virtualizingStackpanel) && (Orientation.Horizontal == virtualizingStackpanel.Orientation)))
                        {
                            scrollInfo.SetHorizontalOffset(index - Math.Floor(scrollInfo.ViewportWidth / 2));
                        }
                        else
                        {
                            scrollInfo.SetVerticalOffset(index - Math.Floor(scrollInfo.ViewportHeight / 2));
                        }
                    }
                }

                else
                {
                    Rect rect = new Rect(new Point(), container.RenderSize);

                    FrameworkElement constrainingParent = container;
                    do
                    {
                        constrainingParent = VisualTreeHelper.GetParent(constrainingParent) as FrameworkElement;
                    } while ((null != constrainingParent) && (listBox != constrainingParent) && !(constrainingParent is ScrollContentPresenter));

                    if (null != constrainingParent)
                    {
                        rect.Inflate(Math.Max((constrainingParent.ActualWidth - rect.Width) / 2, 0), Math.Max((constrainingParent.ActualHeight - rect.Height) / 2, 0));
                    }

                    container.BringIntoView(rect);
                }
            }

Using some other codes instead of this, I can select the items, still hard(I click on the item and wait a few seconds until it will be pressed, not the one I choose, but some other from that position, hope to make myself clear).

So it is something wrong with this code that prevents me to select an item before it reaches 100 ? Or is there a programming way to fix my problem ?

Hope you guys understand my problem. Thanks.

Upvotes: 1

Views: 450

Answers (1)

Security Hound
Security Hound

Reputation: 2551

So it is something wrong with this code that prevents me to select an item before it reaches 100 ? Or is there a programming way to fix my problem ?

The reason your unable to select an item is because the method your using to add items to the list is blocking that behavior. This is the reason your other method allows you to select an item ( the wait is from the blocking behavior ) but the item selected is the same item you selected while the list was being created. In other words the item was in slot 50 but the item you really want was moved down to slot 60.

Since you didn't post how your adding items to the ListBox this is the best I can do.

Upvotes: 1

Related Questions