WPFアプリケーション(Framework 3.5 SP1)のTreeViewに問題があります。これは、2つのレベルのデータを持つTreeVIewです。最初のレベルのアイテムを特定の方法で展開/折りたたみます(TreeViewItemをマウスで1回クリックするだけです)。ここでも、第1レベルのTreeViewItemを展開するときに、第2レベルのTreeViewItemをグループに追加します(これは重要な詳細です。実際、アイテムを追加しなくても問題は発生しません)。TreeViewがフォーカスを失うまで、すべてが正常に機能します。たとえば、最初の位置でTreeViewItemを展開し、同時に1つの要素を2番目のレベルに追加する場合は、ボタンをクリックして(TreeViewのフォーカスを失わせるため)、もう一度クリックします。 3番目の位置にあるTreeViewItemは、マウスの位置でのヒットテストの結果であるTreeViewItemは「実際の」ものではありません。TreeViewItem(この場合は3番目)ですが、クリックされたもの(この場合は2番目)よりも高い位置にあるTreeViewItemです。TreeView-LostFocusイベントでUpdateLayoutメソッドを使用しようとしましたが、結果がありません。おそらく、反対のことを行うメソッドが必要です。UIから始めて、TreeViewItemsの位置を含むオブジェクトを更新します。手伝ってくれませんか?ありがとうございました!ピレッギ
これはコードです:
' 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