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. 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。

    // ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...

  2. 深入Asyncio(一)入门介绍

    介绍 Asyncio试图解决什么问题? 对于IO负载,有且仅有两个理由使用基于asyncio的并发而不是基于多线程的并发: 1. Asyncio为抢占式多任务(线程)提供了一个更安全的替代方案,避免了 ...

  3. Hive总结(四)hive安装记录

    本篇为安装篇较简单: 前提: 1: 安装了hadoop-1.0.4(1.0.3也能够)正常执行 2:安装了hbase-0.94.3, 正常执行 接下来,安装Hive,基于已经安装好的hadoop.过程 ...

  4. EasyPlayer RTSP Android安卓播放器实现视频源快速切换

    EasyPlayer现在支持多视频源快速切换了,我们介绍一下是如何实现的. 这个需求通常应用在一个客户端需要查看多个视频源的情况,比如多个监控场景轮播. 由于EasyPlayer的播放端已经放在Fra ...

  5. 如何利用Windows System Image Manager制作一个answer file

    打开Windows System Image Manager 从菜单中创建一个新的Answer File 在窗体左下部的Windows Image处右键,选择菜单Select Windows Imag ...

  6. error LNK2022: metadata operation failed (801311D6) : Differing number of methods in duplicated types

    本文主要是记录一个C++编译错误的解决方案,具体错误请看本文标题. 这个错误主要是由Managed C++的增量编译导致的,这是VS 2008的一个bug,在VS 2010已经修复,我使用的正式201 ...

  7. 我的Android进阶之旅------>android视频播放只有声音无画面的解决办法

    今天调试公司用VideoView实现的播放器来播放视频的时候,只有声音输出而无画面输出.一开始以为是自己程序有问题,调试了半天无果.怀疑是真机本身的问题,于是下了几个第三方的播放器来进行视频播放,例如 ...

  8. 关于android R.java文件无法创建的问题

    R.java文件无法创建的原因网上有很多说法普遍是以下两种: 1. xml文件有错误: 解决方法就是找到哪个xml有错然后把错误修复就OK了. 2.编码问题 这时候只要把xml文件的编码改成utf8就 ...

  9. ZOJ - 3866 Cylinder Candy 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3866 思路 积分 参考博客 https://blog.csdn. ...

  10. cinder挂载卷的过程原理

    LVM名称介绍 PV:物理磁盘 VG:卷组,多个PV可以组成一个VG,一个VG可以划分成多个LV(逻辑卷). PP:物理区块,将一个VG逻辑的划分成连续的小块. LP:逻辑区块,若干个PP组成一个LP ...