Note that there are some explanatory texts on larger screens.

plurals
  1. POAfter rebinding Silverlight Listbox control how do you get it listbox to scroll to back to the top?
    primarykey
    data
    text
    <p>I have a silverlight listbox that is being used as a search result box. I'm using dynamic searching (keyups in the search box cause the events to fire to filter this list box's contents). The issue I'm running into is if the user scrolls down when the box is unfiltered, then does the search, the rebinding of the listbox does not cause the scroll to go back up to the top making the results look like there is only one value in it.</p> <p>the code I have so far for the listbox is this (This is a simplified version):</p> <p>XAML:</p> <pre><code>&lt;Grid x:Name="MainGrid" Rows="2"&gt; &lt;StackPanel Orientation="Horizontal" Grid.Row="0"&gt; &lt;TextBlock text="Search" Grid.Row="0" /&gt; &lt;Textbox x:name="textboxSearch" Keyup="textBoxSearch_KeyUp" width="200" Height="25"/&gt; &lt;/StackPanel&gt; &lt;ListBox x:Name="SearchResultBox" Visibility="Visible" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalscrollbarVisibility="Auto"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock Text="{Binding ReportName}" /&gt; &lt;TextBlock Text="{Binding ReportDescription}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Listbox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p>VB:</p> <pre><code>Imports System.Threading Imports System.Collections.ObjectModel Imports System.ComponentModel Partial Public Class ucSearch Inherits UserControl Private WithEvents BGwork As New BackgroundWorker() Private mReportList as New List(Of cFilter) Public Sub New() InitializeComponent() FillReportList() NewFilterList() End Sub Private Sub FillReportList() mReportList.Add(new cFilter("Report A", "Report A desc") mReportList.Add(new cFilter("Report B", "Report B desc") mReportList.Add(new cFilter("Report C", "Report C desc") mReportList.Add(new cFilter("Report D", "Report D desc") mReportList.Add(new cFilter("Report E", "Report E desc") mReportList.Add(new cFilter("Report F", "Report F desc") mReportList.Add(new cFilter("Report G", "Report G desc") mReportList.Add(new cFilter("Report H", "Report H desc") mReportList.Add(new cFilter("Report I", "Report I desc") mReportList.Add(new cFilter("Report J", "Report J desc") mReportList.Add(new cFilter("Report K", "Report K desc") mReportList.Add(new cFilter("Report L", "Report L desc") mReportList.Add(new cFilter("Report M", "Report M desc") End Sub Private Sub textboxSearch_KeyUp(ByVal sender as System.Object, _ ByVal e as System.Windows.Input.KeyeventArgs) NewFilterList() End Sub Private Sub NewFilterList() If BGwork.IsBusy Then If Not BGWork.cancellationPending Then BGwork.CancelAsync() Exit Sub End If With BGwork .WorkerSupportsCancellation = True .RunWorkerAsync(textboxSearch.Text) End With End Sub Private Sub BGwork_DoWork(ByVal sender as Object, _ ByVal e as System.ComponentModel.DoWorkEventArgs) _ Handles BGwork.DoWork Dim Filtered as New List(of cFilter) If textboxSearch.Text.Length &gt; 0 dim q = FROM ri In mReportList Where ri.Reportname.ToLower.contains(textboxSearch.Text.ToLower) Select ri Filtered = q Else Filtered = mReportList End If Dim RTN as List(Of cFilter) = Filtered e.Cancel = False e.Result = RTN End Sub Private Sub BGwork_RunWorkerCompleted(ByVal sender As Object_ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BGwork.RunWorkerCompleted If e.Cancelled Then NewFilterList() Exit Sub End If Dim RTN as cFilter = TryCast(e.Result, cFilter) If IsNothing(RTN) Then Exit Sub SearchResultBox.ItemsSource = RTN SearchResultBox.InvalidateArrange() End Sub End Class Public Class cFilter Inherits BaseDataClass Private mReportName as String = "" Private mReportDescription as String = "" Public Sub New() mReportName = "" mReportDescription = "" End Sub Public Sub New(ByVal reportName as String, ByVal reportDescription as String) mReportName = reportName mReportDescription = reportDescription End Sub Public Property ReportName() as String Get Return mReportName End Get Set(ByVal value as String) mReportName = value End Set End Property Public Property ReportDescription() as String Get Return mReportDescription End Get Set(ByVal value as String) mReportDescription = value End Set End Property End Class </code></pre> <p>Again this is simplified greatly from what is going on (I go to database to get report names, etc). When I rebind the listbox, how do I get it to scroll all the way to have the first item at the top of the list? Since I don't have access to the scrollviewer control from within the ListBox object, do I have to make a scrollviewer control that surrounds the listbox and then set where it's view is from there?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload