重新绑定Silverlight Listbox控件之后如何让它列表框滚动到顶部?

重新绑定Silverlight Listbox控件之后如何让它列表框滚动到顶部?,第1张

概述我有一个用作搜索结果框的silverlight列表框.我正在使用动态搜索(搜索框中的键盘会导致触发事件以过滤此列表框的内容).我遇到的问题是如果用户在未经过滤的情况下向下滚动,然后进行搜索,则重新绑定列表框不会导致滚动返回到顶部,使得结果看起来只有一个它的价值. 我到目前为止列表框的代码是这个(这是一个简化版本): XAML: <Grid x:Name="MainGrid" Rows="2"> 我有一个用作搜索结果框的silverlight列表框.我正在使用动态搜索(搜索框中的键盘会导致触发事件以过滤此列表框的内容).我遇到的问题是如果用户在未经过滤的情况下向下滚动,然后进行搜索,则重新绑定列表框不会导致滚动返回到顶部,使得结果看起来只有一个它的价值.

我到目前为止列表框的代码是这个(这是一个简化版本):

XAML:

<GrID x:name="MainGrID" Rows="2">    <StackPanel OrIEntation="Horizontal" GrID.Row="0">         <TextBlock text="Search" GrID.Row="0" />         <TextBox x:name="textBoxSearch" Keyup="textBoxSearch_KeyUp" wIDth="200"                   Height="25"/>    </StackPanel>    <ListBox x:name="SearchResultBox" Visibility="Visible" GrID.Row="1"             ScrollVIEwer.HorizontalScrollbarVisibility="auto"             ScrollVIEwer.VerticalscrollbarVisibility="auto">         <ListBox.ItemTemplate>              <DataTemplate>                   <StackPanel OrIEntation="Vertical">                        <TextBlock Text="{Binding Reportname}" />                        <TextBlock Text="{Binding ReportDescription}" />                   </StackPanel>              </DataTemplate>         </ListBox.ItemTemplate>    </ListBox></GrID>

VB:

imports System.Threadingimports System.Collections.ObjectModelimports System.ComponentModelPartial 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 > 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

再一次,这与发生的事情大大简化了(我去数据库获取报告名称等).当我重新绑定列表框时,如何让它一直滚动以使第一个项目位于列表顶部?由于我无法访问ListBox对象中的scrollvIEwer控件,我是否必须创建一个围绕列表框的scrollvIEwer控件,然后设置它的视图位置?

解决方法 看完这篇文章后

Automatic Scrolling in a Silverlight List Box

我尝试了以下内容,它对我来说很好.

theListBox.ItemsSource = data; theListBox.UpdateLayout(); theListBox.ScrollintoVIEw(theListBox.Items[0]);
总结

以上是内存溢出为你收集整理的重新绑定Silverlight Listbox控件之后如何让它列表框滚动到顶部?全部内容,希望文章能够帮你解决重新绑定Silverlight Listbox控件之后如何让它列表框滚动到顶部?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1004839.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存