treeviewitem을 검색하고 선택할 수있는 검색 텍스트 상자가 있습니다. treeview의 하위 항목은 지연로드됩니다.WPF에서 TreeviewItem 선택
검색 텍스트 상자에 전달 된 트리 뷰의 하위 시간을 선택하고 싶습니다.
어떻게해야합니까?
감사 샤 라트 여기 트 리뷰의 항목을 선택하는 방법에 대한 좋은 정보가있다
treeviewitem을 검색하고 선택할 수있는 검색 텍스트 상자가 있습니다. treeview의 하위 항목은 지연로드됩니다.WPF에서 TreeviewItem 선택
검색 텍스트 상자에 전달 된 트리 뷰의 하위 시간을 선택하고 싶습니다.
어떻게해야합니까?
감사 샤 라트 여기 트 리뷰의 항목을 선택하는 방법에 대한 좋은 정보가있다
내 도우미, 추출물 필요한 것 : 추신. 나는 ExpandAll 메서드를 테스트 한 적이 없다.
Imports System.Runtime.CompilerServices
Imports System.Windows.Controls.Primitives
<HideModuleName()>
Module TreeViewHelper
<Extension()>
Public Sub SetSelectedItem(ByVal treeView As TreeView, ByVal item As Object)
SetSelectedItemInternal(treeView, item)
End Sub
<Extension()>
Public Sub SetSelectedItem(ByVal treeViewItem As TreeViewItem,
ByVal item As Object)
SetSelectedItemInternal(treeViewItem, item)
End Sub
Private Sub SetSelectedItemInternal(ByVal control As ItemsControl,
ByVal item As Object)
ValidateItemsControl(control)
If item Is Nothing Then Throw New ArgumentNullException("item")
If Not control.Items.Contains(item) Then _
Throw New ArgumentOutOfRangeException("item",
"Specified item was not found in the control.")
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
DirectCast(control.ItemContainerGenerator.ContainerFromItem(item),
TreeViewItem).IsSelected = True
Else
Dim selectWhenReadyMethod As EventHandler
selectWhenReadyMethod =
Sub(sender, e)
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
RemoveHandler control.ItemContainerGenerator.StatusChanged,
selectWhenReadyMethod
SetSelectedItemInternal(control, item)
End If
End Sub
AddHandler control.ItemContainerGenerator.StatusChanged,
selectWhenReadyMethod
End If
End Sub
<Extension()>
Public Sub SetSelectedIndex(ByVal treeView As TreeView, ByVal index As Object)
SetSelectedIndexInternal(treeView, index)
End Sub
<Extension()>
Public Sub SetSelectedIndex(ByVal treeViewItem As TreeViewItem,
ByVal index As Object)
SetSelectedIndexInternal(treeViewItem, index)
End Sub
Private Sub SetSelectedIndexInternal(ByVal control As ItemsControl,
ByVal index As Object)
ValidateItemsControl(control)
If index < 0 OrElse index > control.Items.Count - 1 Then _
Throw New ArgumentOutOfRangeException("index")
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
DirectCast(control.ItemContainerGenerator.ContainerFromIndex(index),
TreeViewItem).IsSelected = True
Else
Dim selectWhenReadyMethod As EventHandler
selectWhenReadyMethod =
Sub(sender, e)
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
RemoveHandler control.ItemContainerGenerator.StatusChanged,
selectWhenReadyMethod
SetSelectedIndexInternal(control, index)
End If
End Sub
AddHandler control.ItemContainerGenerator.StatusChanged,
selectWhenReadyMethod
End If
End Sub
<Extension()>
Public Sub ExpandAll(ByVal treeView As TreeView)
ExpandAllInternal(treeView)
End Sub
<Extension()>
Public Sub ExpandAll(ByVal treeViewItem As TreeViewItem)
ExpandAllInternal(treeViewItem)
End Sub
Private Sub ExpandAllInternal(ByVal control As ItemsControl)
ValidateItemsControl(control)
If control.Items.IsEmpty Then Exit Sub
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
For i = 0 To control.Items.Count - 1
Dim item =
DirectCast(control.ItemContainerGenerator.ContainerFromIndex(i),
TreeViewItem)
item.IsExpanded = True
ExpandAllInternal(control)
Next
Else
Dim expandWhenReadyMethod As EventHandler
expandWhenReadyMethod =
Sub(sender, e)
If control.ItemContainerGenerator.Status =
GeneratorStatus.ContainersGenerated Then
RemoveHandler control.ItemContainerGenerator.StatusChanged,
expandWhenReadyMethod
ExpandAllInternal(control)
End If
End Sub
AddHandler control.ItemContainerGenerator.StatusChanged,
expandWhenReadyMethod
End If
End Sub
Private Sub ValidateItemsControl(ByVal control As ItemsControl)
If control Is Nothing Then Throw New ArgumentNullException("control")
If Not TypeOf control Is TreeView AndAlso
Not TypeOf control Is TreeViewItem Then _
Throw New _
NotSupportedException(control.GetType.FullName & " is not supported.")
End Sub
End Module