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