2010-07-14 4 views
0

WPF 애플리케이션 (프레임 워크 3.5 SP1)에서 TreeView에 문제가 있습니다. 두 가지 수준의 데이터가있는 TreeVIew입니다. 첫 번째 레벨의 항목을 특정 방식으로 확장/축소합니다 (TreeViewItem에서 한 번의 마우스 클릭으로). 다시 첫 번째 수준의 TreeViewItem을 확장하면 두 번째 수준의 TreeViewItem을 그룹에 추가합니다 (문제가 발생하지 않는 항목을 추가하지 않으면 실제로 중요한 세부 정보가 표시됨). TreeView가 포커스를 잃을 때까지 모든 것이 올바르게 작동합니다. 예를 들어 첫 번째 위치에서 TreeViewItem을 확장하고 동시에 두 번째 수준에 요소 하나를 추가 한 다음 단추를 클릭하면 TreeView에서 포커스를 잃게됩니다. 그런 다음 다시 클릭합니다. 세 번째 위치에있는 TreeViewItem을 확장하면 마우스 위치에 대한 적중 테스트의 결과 인 TreeViewItem이 "실제"TreeViewItem (이 경우 세 번째)이 아니라 TreeViewItem보다 높은 위치에있는 TreeViewItem 클릭 (이 경우 두 번째). TreeView-LostFocus 이벤트에서 UpdateLayout 메서드를 사용하려고했지만 결과가 없습니다. 아마 나는 반대 방법을 필요로 : UI에서 시작, TreeViewItems의 위치를 ​​포함하는 개체를 새로 고치십시오. 도와 주시겠습니까? 감사합니다.WPF 새로 고침 TreeView가 포커스를 잃을 때

' 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

내가 (하지만 난 그것을 매우 좋아하지 않는다)이 해결책을 찾을 수 있습니다 PILEGGI

이것은 코드입니다. 문제는 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 
관련 문제