WPF之TreeView查找节点

 private void tbDeviceType_TextChanged(object sender, TextChangedEventArgs e)
        {
            #region  源代码
            //if (tbDeviceType.Text == "")
            //{
            //    TreeViewTool.ItemsSource = listBoxItems;
            //}
            //else
            //{
            //    List<ListBoxItem> tempItems = new List<ListBoxItem>();
            //    foreach(var v in listBoxItems)
            //    {                  
            //        if(v.DisplayName.ToUpper().Contains(tbDeviceType.Text.ToUpper()) == true )
            //        {
            //            tempItems.Add(v);
            //        }
            //    }
            //    if(tempItems.Count == 0)
            //    {
            //        tempItems.Add(new ListBoxItem() { DisplayName = "Device Not Found." });
            //    }
            //    else
            //    {
            //    }
            //    TreeViewTool.ItemsSource = tempItems;

            //} 
            #endregion
            ListBoxItem item = null;
            string searchname = this.tbDeviceType.Text;
            foreach (var v in listBoxItems)
            {
                if (v.DisplayName.Contains(searchname))//模糊查询
                //if(v.DisplayName == searchname)
                {
                    item = v;
                    break;
                }
            }
            for (int i = 0; i < TreeViewTool.Items.Count; i++)
            {
                ListBoxItem v = TreeViewTool.Items.GetItemAt(i) as ListBoxItem;
                PareItems(TreeViewTool, v, item);
            }
             
        }

        //DFS搜索
        private void PareItems(ItemsControl itemsControl, ListBoxItem item, ListBoxItem TT)
        {
            TreeViewItem container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

            if (container != null)
            {
                container.IsExpanded = true;

                if (container.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                {
                    container.UpdateLayout();
                }
                if ((container.Header as ListBoxItem).Children != null)
                {
                    foreach (var it in (container.Header as ListBoxItem).Children)
                    {
                        PareItems(container, it, TT);
                    }
                }

                if ((container.Header as ListBoxItem) == TT)    //TT要找的子元素
                {

                    container.IsSelected = true;
                    container.BringIntoView();//滚动条滚动到选中的子元素
                }

                itemsControl = container;
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值