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

发布于 2024-08-13 05:16:46 字数 5261 浏览 5 评论 0原文

我有一个 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.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 > 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 对象内访问滚动查看器控件,因此我是否必须创建一个围绕列表框的滚动查看器控件,然后设置其视图的位置?

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.

the code I have so far for the listbox is this (This is a simplified version):

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.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 > 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-08-20 05:16:46

看了这篇文章

在 Silverlight 列表框中自动滚动

后,我尝试了以下内容对我来说效果很好。

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

After looking at this post

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]);
爱情眠于流年 2024-08-20 05:16:46

你试过吗

mReportList.SelectedIndex = 0

did you try

mReportList.SelectedIndex = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文