tags:

views:

18

answers:

1

The Silverlight ListBox control automatically moves the scroll box when the list item shown is clicked (if the list box is showing 5 items, click on the last item and all of the items are moved down one). I have a bug from my QA team telling me that causes confusion for our specific case. How can I override this behavior?

    <ListBox x:Name="accountsListBox" Margin="8,65,8,8" SelectionChanged="accountsListBox_SelectionChanged" VirtualizingStackPanel.VirtualizationMode="Recycling">
                    <ListBox.BorderBrush>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="0"/>
                            <GradientStop Color="Silver" Offset="1"/>
                        </LinearGradientBrush>
                    </ListBox.BorderBrush>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Height="19" Text="{Binding}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> names = new List<string>();
            for (int i = 0; i < 100; i++)
            {
                names.Add("Name " + i);
            }

            this.accountsListBox.ItemsSource = names;

        }

        private void accountsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
// this was an attempt but causes other unwanted behavior
            //int selectedIndex = this.accountsListBox.SelectedIndex;
            //this.accountsListBox.UpdateLayout();
            //this.accountsListBox.ScrollIntoView(this.accountsListBox.Items[this.accountsListBox.SelectedIndex - 4]);
            //this.accountsListBox.SelectedIndex = selectedIndex;
        }
    }
+2  A: 

There is no need to override the behaviour. That is not a bug, but a feature for scrolling partially displayed list items into view.

The auto-scrolling behavior is caused by the list box being not quite tall enough for the 5 items (even though it may look it). Make the list box a few pixels taller and the problem will go away.

Hope this helps.

Enough already
That's exactally right! Thanks for seeing what I didn't.
Kevin