0

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
4

1 に答える 1

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
于 2010-07-14T15:50:18.790 に答える