zoukankan      html  css  js  c++  java
  • WPF TreeView 后台C#选中指定的Item, 需要遍历

      
            private TreeViewItem FindTreeViewItem(ItemsControl container, object item)
            {
                if (null == container || null == item)
                {
                    return null;
                }

                if (container.DataContext == item)
                {
                    return container as TreeViewItem;
                }

                int count = container.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    TreeViewItem subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);

                    if (null == subContainer)
                    {
                        continue;
                    }

                    // Search the next level for the object.
                    TreeViewItem resultContainer = FindTreeViewItem(subContainer, item);
                    if (null != resultContainer)
                    {
                        return resultContainer;
                    }
                }

                return null;
            }



    /////使用上述方法遍历 , 然后设置IsSelected属性

       private static void SelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                //
                SelectedProtocolControl spc = d as SelectedProtocolControl;

                if (null != spc)
                {
                    if (e.NewValue != spc.trvSelectedProtocol.SelectedItem)
                    {
                        //TreeViewItem tviNew = spc.trvSelectedProtocol.ItemContainerGenerator.ContainerFromItem(e.NewValue) as TreeViewItem;
                        TreeViewItem tviNew = spc.FindTreeViewItem(spc.trvSelectedProtocol, e.NewValue);
                        if (null != tviNew)
                        {
                            tviNew.IsSelected = true;
                        }
                    }

                    if (null != e.OldValue)
                    {
                        //TreeViewItem tviOld = spc.trvSelectedProtocol.ItemContainerGenerator.ContainerFromItem(e.OldValue) as TreeViewItem;
                        TreeViewItem tviOld = spc.FindTreeViewItem(spc.trvSelectedProtocol, e.OldValue);
                        if (null != tviOld)
                        {
                            tviOld.IsSelected = false;
                        }
                    }
                }

                System.Diagnostics.Debug.WriteLine("SelectedItemChanged...");
            }





    https://muzizongheng.blog.csdn.net/
  • 相关阅读:
    Taro文件上传:Blob Url下载Blob对象本身并通过接口上传到服务器
    Taro项目遇到的问题
    RPC是什么?
    句柄
    正向代理和反向代理
    Go Micro搭建简单微服务
    gRPC奇怪的编译命令protoc
    官网下载Git方法
    Go/golang:解决依赖包模块安装问题
    URL
  • 原文地址:https://www.cnblogs.com/muzizongheng/p/3166601.html
Copyright © 2011-2022 走看看