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...");
        }

WPF TreeView 后台C#选中指定的Item, 需要遍历的更多相关文章

  1. WPF TreeView SelectedItemChanged called twice

    How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...

  2. C# WPF 实现鼠标固定在指定范围内运动

    原文:C# WPF 实现鼠标固定在指定范围内运动   一.背景: 需要实现带有三个屏幕,三个屏幕分别显示窗体,但鼠标只能在主窗体中运动,不能移动到其他的两个附屏中.   二.实现: 具体实现使用的是u ...

  3. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

  4. [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件

    原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...

  5. JavaScript- 获得TreeView CheckBox里选中项的值

    获得TreeView CheckBox里选中项的值,对JSDOM控制还不是很熟,感觉不太容易.试了很多次终于成功了. 代码如下 <body> <form id="form1 ...

  6. TreeView 数据绑定及选中命令处理

    昨天接近下班,一个群里面的网友,问treeView绑定后  选中命令怎么来处理,怎么没有效果,而且用MVVM的方式来写:快下班了本来想远程帮他看下,结果就说写个Demo给他看:再加上选中传参: 下面分 ...

  7. 一个滑动选中RecyclerView中Item的布局SlidingCheckLayout,手指滑过Item时多项选中。

    SlidingCheckLayout是一个滑动选中RecyclerView中Item的布局,手指滑过Item时多项选中. 作者:竹尘居士 github:https://github.com/homgw ...

  8. WPF绑定文本时使用指定格式文本

    原文:WPF绑定文本时使用指定格式文本 Text="{Binding PlayletModel.characters,StringFormat=Cast : {0}}" Strin ...

  9. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

随机推荐

  1. spring中xml配置和autowired混用

    1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a ...

  2. ubantu 下 修改mysql 默认编码

    启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...

  3. windows 10 python 2.7和python3.6共存解决方法和pip安装

    一.首先去python官网将两个版本下载并安装: 然后进入windows的环境变量,检查下面4个变量: 1.C:\Python272.C:\Python27\Scripts3.D:\software\ ...

  4. c# 根据枚举Value 获得名称

    // 定义枚举类型enum sotype : int { book=1, pen=2, other=3 } // 输出名称 switch (Enum.GetName(typeof(sotype), 1 ...

  5. python 基础 8.1 r 正则对象

                                                                                                        ...

  6. 【BZOJ1150】[CTSC2007]数据备份Backup 双向链表+堆(模拟费用流)

    [BZOJ1150][CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此 ...

  7. C#中反射type记录

    写代码的时候经常需要使用反射相关的东西例如:分析现有类型自动生成类, 或者为现有的类自动增加一些功能总结了一点点经验以ClassA  a; 为例1. 通过typeof(ClassA) 或者 a.Get ...

  8. 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称

    说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...

  9. SSL:Ubuntu证书配置

    CA证书的配置 Ubuntu上CA证书的配置可以通过工具ca-certificates来方便的进行.该工具默认是随Ubuntu安装的,如果没有可以通过下面的命令来安装: sudo apt-get in ...

  10. 我的Java开发学习之旅------>使用循环递归算法把数组里数据数组合全部列出

    面试题如下:把一个数组里的数组合全部列出,比如1和2列出来为1,2,12,21. (面试题出自<Java程序员面试宝典>) 代码如下: import java.util.Arrays; i ...