我尝试使用计时器事件同步以下3个列表框。
左: ID
中间: Listbox1
右: Listbox2
Private GI_Sync_X_centerLine As Integer
GI_Sync_X_centerLine = (ListBox1.Left + ListBox1.Width + ListBox1.Left) \ 2 (here should be the issue)
Private Sub sync_Tick(sender As Object, e As EventArgs) Handles sync.Tick
If (ListBox1.TopIndex <> ListBox2.TopIndex) Then ' out-of-sync
If (ListBox1.Items.Count = ListBox2.Items.Count) Then ' both are scrollable
If ((MousePosition.X - Me.Location.X) < GI_Sync_X_centerLine) Then ' mouse is on the left one
ListBox2.TopIndex = ListBox1.TopIndex ' move the right one to match
Else
ListBox1.TopIndex = ListBox2.TopIndex ' & v.v.
End If
End If
End If
If (id.TopIndex <> ListBox2.TopIndex) Then ' out-of-sync
If (id.Items.Count = ListBox2.Items.Count) Then ' both are scrollable
If ((MousePosition.X - Me.Location.X) < GI_Sync_X_centerLine) Then ' mouse is on the left one
ListBox2.TopIndex = id.TopIndex ' move the right one to match
Else
id.TopIndex = ListBox2.TopIndex ' & v.v.
End If
End If
End If
If (id.TopIndex <> ListBox1.TopIndex) Then ' out-of-sync
If (id.Items.Count = ListBox1.Items.Count) Then ' both are scrollable
If ((MousePosition.X - Me.Location.X) < GI_Sync_X_centerLine) Then ' mouse is on the left one
ListBox1.TopIndex = id.TopIndex ' move the right one to match
Else
id.TopIndex = ListBox1.TopIndex ' & v.v.
End If
End If
End If
End Sub
在https://social.msdn.microsoft.com/Forums/vstudio/en-US/38413d0a-7388-4191-a7a6-fd66e469d502/two-listbox-scrollbar-in-synchronisation?forum=wpf找到解决方案,并将其修改为3个列表框。
问题如下:
如果我滚动左侧列表框,其他所有其他人也会滚动。 如果我滚动右侧列表框,其他所有其他滚动, 但是,如果我尝试滚动中间列表框,其他人不会滚动,而中间会卡在其他人所在的位置。
答案 0 :(得分:0)
Public Function GetDescendantByType(element As Visual, type As Type) As Visual
If element Is Nothing Then
Return Nothing
End If
If element.[GetType]() = type Then
Return element
End If
Dim foundElement As Visual = Nothing
If TypeOf element Is FrameworkElement Then
TryCast(element, FrameworkElement).ApplyTemplate()
End If
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(element) - 1
Dim visual As Visual = TryCast(VisualTreeHelper.GetChild(element, i), Visual)
foundElement = GetDescendantByType(visual, type)
If foundElement IsNot Nothing Then
Exit For
End If
Next
Return foundElement
End Function
Private Sub lb_ScrollChanged(sender As Object, e As ScrollChangedEventArgs)
Dim _listboxScrollViewer1 As ScrollViewer = TryCast(GetDescendantByType(id, GetType(ScrollViewer)), ScrollViewer)
Dim _listboxScrollViewer2 As ScrollViewer = TryCast(GetDescendantByType(listBox1, GetType(ScrollViewer)), ScrollViewer)
Dim _listboxScrollViewer3 As ScrollViewer = TryCast(GetDescendantByType(listBox2, GetType(ScrollViewer)), ScrollViewer)
_listboxScrollViewer2.ScrollToVerticalOffset(_listboxScrollViewer1.VerticalOffset)
_listboxScrollViewer3.ScrollToVerticalOffset(_listboxScrollViewer2.VerticalOffset)
_listboxScrollViewer1.ScrollToVerticalOffset(_listboxScrollViewer3.VerticalOffset)
End Sub
并将lb_ScrollChanged与事件ScrollViewer.ScrollChanged="lb_ScrollChanged"
关联到每个ListBox
如果您对该代码有疑问,请回复。