WPF TreeView tools

本文介绍了一组用于WPF TreeView的扩展工具,包括选择、展开节点等功能,并修复了延迟绑定的问题。

在WPF的TreeView使用方式和WinForm下有很大不同,那些展开某节点、获取父节点,判断某节点是否被选中等常用的操作在WinForm下都有相关函数,而在WPF中却不能轻易实现。

一种常规的方式是通过MVVM模式来将TreeViewItem节点中的IsSelect,IsExpanded等属性来双向绑定到要显示的节点数据中,然后直接通过节点数据的属性来实现相关操作。

但是,有的时候,当我们没有ViewModel层,但又想像WinFrom那样直接简单的获取或设置这些属性的时候,该如何办呢。其实WPF还是提供了类似WinForm中的这些设置的,只不过形式不一样了而已,但是却没WinFrom的那么直观和方便。CodeProject上就有人将常用函数总结了一下,写成了扩展函数,主要提供如下功能:

public static void SelectObject(this TreeView treeView, object obj)
public static void SelectObject(this TreeView treeView, object obj, bool selected)
public static bool IsObjectSelected(this TreeView treeView, object obj)
public static bool IsObjectFocused(this TreeView treeView, object obj)
public static void ExpandObject(this TreeView treeView, object obj)
public static void ExpandObject(this TreeView treeView, object obj, bool expanded)
public static bool IsObjectExpanded(this TreeView treeView, object obj)
public static TreeViewItem GetParentItem(this TreeViewItem item)

文章地址如下:WPF TreeView tools

但是,这里面有一个小bug:当TreeView节点中使用延迟绑定的时候,根据数据节点获取TreeItem会失败。这里我把它修正了一下,感兴趣的朋友可以直接使用我修改后的函数。  

代码
     public   static   class  TreeViewTools
    {
        
///   <summary>
        
///  Returns the TreeViewItem of a data bound object.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <returns> The TreeViewItem of the data bound object or null. </returns>
         public   static  TreeViewItem GetItemFromObject( this  TreeView treeView,  object  obj)
        {
            
try
            {
                DependencyObject dObject 
=  GetContainerFormObject(treeView, obj);
                TreeViewItem tvi 
=  dObject  as  TreeViewItem;
                
while  (tvi  ==   null )
                {
                    dObject 
=  VisualTreeHelper.GetParent(dObject);
                    tvi 
=  dObject  as  TreeViewItem;
                }
                
return  tvi;
            }
            
catch  { }
            
return   null ;
        }

        
private   static  DependencyObject GetContainerFormObject(ItemsControl item,  object  obj)
        {
            
if  (item  ==   null )
                
return   null ;

            DependencyObject dObject 
=   null ;
            dObject 
=  item.ItemContainerGenerator.ContainerFromItem(obj);

            
if  (dObject  !=   null )
                
return  dObject;

            var query 
=  from childItem  in  item.Items.Cast < object > ()
                        let childControl 
=  item.ItemContainerGenerator.ContainerFromItem(childItem)  as  ItemsControl
                        select GetContainerFormObject(childControl, obj);

            
return  query.FirstOrDefault(i  =>  i  !=   null );
        }

        
///   <summary>
        
///  Selects a data bound object of a TreeView.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
         public   static   void  SelectObject( this  TreeView treeView,  object  obj)
        {
            treeView.SelectObject(obj, 
true );
        }

        
///   <summary>
        
///  Selects or deselects a data bound object of a TreeView.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <param name="selected"> select or deselect </param>
         public   static   void  SelectObject( this  TreeView treeView,  object  obj,  bool  selected)
        {
            var tvi 
=  treeView.GetItemFromObject(obj);
            
if  (tvi  !=   null )
            {
                tvi.IsSelected 
=  selected;
            }
        }

        
///   <summary>
        
///  Returns if a data bound object of a TreeView is selected.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <returns> Returns true if the object is selected, and false if it is not selected or obj is not in the tree. </returns>
         public   static   bool  IsObjectSelected( this  TreeView treeView,  object  obj)
        {
            var tvi 
=  treeView.GetItemFromObject(obj);
            
if  (tvi  !=   null )
            {
                
return  tvi.IsSelected;
            }
            
return   false ;
        }

        
///   <summary>
        
///  Returns if a data bound object of a TreeView is focused.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <returns> Returns true if the object is focused, and false if it is not focused or obj is not in the tree. </returns>
         public   static   bool  IsObjectFocused( this  TreeView treeView,  object  obj)
        {
            var tvi 
=  treeView.GetItemFromObject(obj);
            
if  (tvi  !=   null )
            {
                
return  tvi.IsFocused;
            }
            
return   false ;
        }

        
///   <summary>
        
///  Expands a data bound object of a TreeView.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
         public   static   void  ExpandObject( this  TreeView treeView,  object  obj)
        {
            treeView.ExpandObject(obj, 
true );
        }

        
///   <summary>
        
///  Expands or collapses a data bound object of a TreeView.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <param name="expanded"> expand or collapse </param>
         public   static   void  ExpandObject( this  TreeView treeView,  object  obj,  bool  expanded)
        {
            var tvi 
=  treeView.GetItemFromObject(obj);
            
if  (tvi  !=   null )
            {
                tvi.IsExpanded 
=  expanded;
                
if  (expanded)
                {
                    
//  update layout, so that following calls to f.e. SelectObject on child nodes will 
                    
//  find theire TreeViewNodes
                    treeView.UpdateLayout();
                }
            }
        }

        
///   <summary>
        
///  Returns if a douta bound object of a TreeView is expanded.
        
///   </summary>
        
///   <param name="treeView"> TreeView </param>
        
///   <param name="obj"> Data bound object </param>
        
///   <returns> Returns true if the object is expanded, and false if it is collapsed or obj is not in the tree. </returns>
         public   static   bool  IsObjectExpanded( this  TreeView treeView,  object  obj)
        {
            var tvi 
=  treeView.GetItemFromObject(obj);
            
if  (tvi  !=   null )
            {
                
return  tvi.IsExpanded;
            }
            
return   false ;
        }

        
///   <summary>
        
///  Retuns the parent TreeViewItem.
        
///   </summary>
        
///   <param name="item"> TreeViewItem </param>
        
///   <returns> Parent TreeViewItem </returns>
         public   static  TreeViewItem GetParentItem( this  TreeViewItem item)
        {
            var dObject 
=  VisualTreeHelper.GetParent(item);
            TreeViewItem tvi 
=  dObject  as  TreeViewItem;
            
while  (tvi  ==   null )
            {
                dObject 
=  VisualTreeHelper.GetParent(dObject);
                tvi 
=  dObject  as  TreeViewItem;
            }
            
return  tvi;
        }
    }

 

 

 

转载于:https://www.cnblogs.com/TianFang/archive/2010/02/06/1665162.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值