在WPF中选择Treeviewitem

时间:2009-05-27 14:51:46

标签: wpf-controls

我有一个搜索文本框来搜索treeviewitem并选择它。树视图中的子项是延迟加载的。

我想选择在搜索文本框中传递的树视图的子时间。

我怎样才能实现这一目标?

由于 沙拉斯

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

这是我的助手,提取你需要的东西: PS。我从未测试过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