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. 小鬼PSer

    我是小鬼PSer,主要修图和做网站的,有需要可以call QQ:1805843351 微信:1805843351

  2. x86 的 TSS 任务切换机制

    转自:http://blog.chinaunix.net/uid-587665-id-2732907.html [0]写在前面 segment descriptors 构建保护模式下的最基本.最根本的 ...

  3. JS 之 数据类型转换

          首先我们来简单了解一下JS中的数据类型,JavaScript拥有字符串.数字.布尔.数组.对象.Null.Undefiend 6中数据类型.同一时候,JavaScript拥有动态类型. 也 ...

  4. 安装配置Django

    安装配置Django 以下是基于python3.5 pip install Django 把python环境目录Scripts配置到环境变量,主要在命令行中随时可以使用django-admin 验证 ...

  5. 【模板】P3806点分治1

    [模板]P3806 [模板]点分治1 很好的一道模板题,很无脑经典. 讲讲淀粉质吧,很营养,实际上,点分治是树上的分治算法.根据树的特性,树上两点的路径只有一下两种情况: 路径经过根\((*)\) 路 ...

  6. 洛谷 4568 [JLOI2011] 飞行路线

    题目戳这里 一句话题意: 有n个点,m条边的有向图,最多可以把k条边变为0,求从起点到终点最短距离. Solution 首先看到这题目,感觉贼难,看起来像DP,貌似也有大佬这么做,但鉴于本蒟蒻思维能力 ...

  7. Java集合(一):Java集合概述

    注:本文基于JDK 1.7 1 概述 Java提供了一个丰富的集合框架,这个集合框架包括了很多接口.虚拟类和实现类. 这些接口和类提供了丰富的功能.可以满足主要的聚合需求. 下图就是这个框架的总体结构 ...

  8. JAVA Exception处理

    原文地址:http://blog.csdn.net/hguisu/article/details/6155636 1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用 ...

  9. windows下创建做一个类似与linux 的SFTP

    在项目中经常需要做通过ftp上传文件到ftp上,如果服务器是windows版的服务器又该如何做呢,下面就给大家介绍一个软件:freeSSHd 软件地址    http://www.freesshd.c ...

  10. .gitignore文件配置:keil工程文件类型【转】

    本文转载自:https://blog.csdn.net/u010160335/article/details/80043965 .gitignore源文件下载链接:git管理keil工程.gitign ...