我在WPF应用程序(Framework 3.5 SP1)中遇到了TreeView问题。 这是一个拥有2个数据级别的TreeVIew。我以特定方式展开/折叠第一级的项目(只需在TreeViewItem上单击鼠标)。再次当我展开第一级TreeViewItem时,我将一些二级TreeViewItem添加到组中(这是一个重要的细节,事实上,如果我不添加项目,则问题不会发生)。一切正常,直到TreeView失去焦点。 例如,如果我在第一个位置展开TreeViewItem,同时将一个元素添加到第二个级别,那么我单击一个按钮(让TreeView失去焦点),然后再次单击TreeViewItem在第三个位置展开它,由鼠标位置命中测试得到的TreeViewItem不是“真正的”TreeViewItem(在本例中是第三个),而是一个位于比单击的位置更高的TreeViewItem (在这种情况下是第二个)。 我试图在TreeView-LostFocus事件上使用UpdateLayout方法,但没有结果。可能我需要一个相反的方法:从UI开始,刷新包含TreeViewItems位置的对象。 你能帮我么? 谢谢! Pileggi
这是代码:
' in this way I tried to put remedy at the problem, but it doesn't work.
Private Sub tvArt_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles tvArt.LostFocus
Me.tvArt.UpdateLayout()
e.Handled = True
End Sub
' here I expand / collapse the items of the first level of my TreeView
Private Sub tvArt_PreviewMouseUp(ByVal sender As System.Object, ByVal e As MouseButtonEventArgs) Handles tvArt.PreviewMouseUp
Dim p As Point = Nothing
Dim tvi As TreeViewItem = getItemFromMousePosition(Of TreeViewItem)(p, e.OriginalSource, Me.tvArt)
If tvi Is Nothing = False Then
If tvi.HasItems Then
Dim be As BindingExpression = BindingOperations.GetBindingExpression(tvi, TreeViewItem.ItemsSourceProperty)
Dim ri As P_RicambiItem = DirectCast(be.DataItem, P_RicambiItem)
If ri.isExpanded = False then
' here I add items to the second level collection
End If
ri.isExpanded = Not ri.isExpanded
End If
End If
e.Handled = True
End Sub
Private Function getItemFromMousePosition(Of childItem As DependencyObject)(ByRef p As Point, ByVal sender As UIElement, _
ByVal _item As UIElement) As childItem
p = sender.TranslatePoint(New Point(0, 0), _item)
Dim obj As DependencyObject = DirectCast(_item.InputHitTest(p), DependencyObject)
While obj Is Nothing = False AndAlso TypeOf obj Is childItem = False
obj = VisualTreeHelper.GetParent(obj)
End While
Return DirectCast(obj, childItem)
End Function
答案 0 :(得分:0)
我找到了这个解决方案(但我不太喜欢它)。问题取决于添加的项目,因为某些原因,wpf不记得存在。然后我用一个清除并重新添加源集合中所有项目的方法进行“手动”刷新:
Public Sub RefreshData(ByVal RicambiListPass As ObservableCollection(Of P_RicambiItem))
Dim l As New List(Of P_RicambiItem)
l.AddRange(RicambiListPass)
_RicambiList.Clear()
For Each i As P_RicambiItem In l
_RicambiList.Add(i)
Next
End Sub