How to make it scroll to the top after resin the Silverlight ListBox control?

I have a silverlight list box that is used as a search result box. I am using dynamic search (the keyboard in the search box will cause an event to be triggered to filter the contents of this list box). I met The problem is that if the user scrolls down without filtering and then performs a search, rebinding the list box will not cause the scroll to return to the top, making the result seem to have only one of its value.

The code for the list box I have so far is this (this is a simplified version):

XAML:




Height="25"/>

ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalscrollbarVisibility="Auto">
< br />







VB:

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(ne w 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.Can celAsync()
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> 0
dim q = FROM ri In mReportList Where ri.Reportname.ToLower.contains(textboxSearch.Text.ToLower) Select ri< br /> 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< br /> 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)
mReport Name = 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

Again, this and what happened is greatly simplified (I go to the database to get the report name, etc.). When I rebind the list box, how do I keep it scrolling so that the first item is at the top of the list? Since I cannot access the scrollviewer control in the ListBox object, do I have to create a scrollviewer control around the list box and then set its view position?

After reading this article

Automatic Scrolling in a Silverlight List Box

I tried the following and it worked fine for me.

theListBox.ItemsSource = data;
theListBox. UpdateLayout();
theListBox.ScrollIntoView(theListBox.Items[0]);

I have a silverlight list box used as a search result box. I am working Use dynamic search (the keyboard in the search box will cause an event to be triggered to filter the contents of this list box). The problem I have is that if the user scrolls down without filtering and then searches, the list is re-bound The box does not cause the scroll to return to the top, making the result seem to have only one value.

The code for the list box I have so far is this (this is a simplified version):

XAML:




Height="25"/>

ScrollViewer.HorizontalScrollBarVisibility=" Auto"
ScrollViewer.Verticalscr ollbarVisibility="Auto">









VB:

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 cFil ter("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> 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 = ""
Priv ate 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< br /> 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

Once again, this and what happened is greatly simplified (I go to the database to get the report name, etc.). When I re-bind When defining a list box, how do I keep it scrolling so that the first item is at the top of the list? Since I cannot access the scrollviewer control in the ListBox object, do I have to create a scrollviewer control around the list box and then set its view position?

After reading this article

Automatic Scrolling in a Silverlight List Box

I try I added the following and it works fine for me.

theListBox.ItemsSource = data;
theListBox.UpdateLayout();
theListBox.ScrollIntoView( theListBox.Items[0]);

Leave a Comment

Your email address will not be published.