WPF在失去焦点时刷新TreeView [英] WPF refresh TreeView when it loses the focus

查看:741
本文介绍了WPF在失去焦点时刷新TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF应用程序(Framework 3.5 SP1)中遇到了我的TreeView的问题。
这是一个TreeVIew有2个级别的数据。我以特定的方式(通过单击鼠标单击TreeViewItem)展开/折叠第一级的项目。再次当我展开一个第一级TreeViewItem,我添加一些第二级TreeViewItems到组(这是一个重要的细节,如果我没有添加项目的问题没有发生)。所有工作良好,直到TreeView失去焦点。
例如,如果我在第一个位置展开TreeViewItem,同时添加一个元素到第二级,然后我点击一个按钮(让TreeView失去焦点),然后我再次单击TreeViewItem在第三个位置展开它,使用鼠标位置的hit-test结果的TreeViewItem不是真正的TreeViewItem(在这种情况下是第三个),而是处于更高位置的TreeViewItem比单击的(在这种情况下第二)。
我试图在TreeView-LostFocus事件上使用UpdateLayout方法,但没有结果。可能我需要一个相反的方法:从UI开始,刷新包含TreeViewItems位置的对象。
你能帮助我吗?
谢谢!
Pileggi

I have a problem with my TreeView in a WPF application (Framework 3.5 SP1). It's a TreeVIew with 2 Levels of Data. I expand / collapse the items of the first level in a particular way (with a single mouse-click on the TreeViewItem). Again when I expand a first-level TreeViewItem, I add some second-level TreeViewItems to the group (it's an important detail, infact if I don't add the items the problem doesn't occur). All works good until the TreeView loses focus. If, for example, I expand the TreeViewItem at the first position, adding at the same time one element to the second-level, then I click on a button (to let the TreeView lose the focus), and then I click again on the TreeViewItem at the third position to expand it, the TreeViewItem that results from the hit-test with the mouse position is not the "real" TreeViewItem (in this case the third), but a TreeViewItem which is in an higher position than the one clicked (in this case the second). I have tried to use the UpdateLayout method on the TreeView-LostFocus event, but without results. Probably I need a method that does the opposite: starting from the UI, refresh the object that contains the position of the TreeViewItems. Can you, please, help me? Thank you! 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


推荐答案

不喜欢它非常)。问题是取决于添加的项目,wpf,由于某些原因,不记得存在。然后我做一个手动刷新方法,清除并重新添加源集合中的所有项目:

I have find this solution (but I don't like it very much). The problem is depending from the added items that wpf, for some reasons, doesn't remember that exist. Then I do a "manual" refresh with a method that clear and re-add all the items in the source-collection:

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

这篇关于WPF在失去焦点时刷新TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆