zoukankan      html  css  js  c++  java
  • Enumerate the visual tree and logic tree

    How can I enumerate all the descendants of a visual object ?    


    You can enumerate all the descendants of a visual object as follows :
    // Enumerate all the descendants of the visual object. 
     
    static public void EnumVisual(Visual myVisual)
     
    {
     
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
     
    {
     
    // Retrieve child visual at specified index value.
     
    Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
     
    // Do processing of the child visual object.
     
    // Enumerate children of the child visual object.
     
    EnumVisual(childVisual);
     
    }
     

    How can I override the Logical Tree ?    


    Although it is not necessary in most cases, advanced control authors have the option to override the logical tree. This example describes how to subclass 'StackPanel' to override the logical tree, in this case to enforce a behavior that the panel may only have and will only render a single child element.

    Normal logical tree can be overridden as follows :

     
    [C#]
     
    public class SingletonPanel : StackPanel
     

     
    private FrameworkElement _child;
     

     
    public SingletonPanel() {
     

     
    }
     

     
    public FrameworkElement SingleChild
     
    {
     

     
    get { return _child;}
     
    set
     
    {
     
    if (value==null) {
     
    RemoveLogicalChild(_child);
     
    } else {
     
    if (_child==null) {
     
    _child = value;
     
    } else {
     
    // raise an exception?
     
    MessageBox.Show("Needs to be a single element");
     
    }
     
    }
     
    }
     
    }
     
    public void SetSingleChild(object child)
     
    {
     
    this.AddLogicalChild(child);
     
    }
     

     
    public new void AddLogicalChild(object child)
     
    {
     
    _child = (FrameworkElement)child;
     
    if (this.Children.Count == 1)
     
    {
     
    this.RemoveLogicalChild(this.Children[0]);
     
    this.Children.Add((UIElement)child);
     
    }
     
    else
     
    {
     
    this.Children.Add((UIElement)child);
     
    }
     
    }
     

     
    public new void RemoveLogicalChild(object child)
     

     
    {
     
    _child = null;
     
    this.Children.Clear();
     
    }
     
    protected override IEnumerator LogicalChildren
     
    {
     
    get {
     
    // cheat, make a list with one member and return the enumerator
     
    ArrayList _list = new ArrayList();
     
    _list.Add(_child);
     
    return (IEnumerator) _list.GetEnumerator();}
     
    }
     
    }
     

     

  • 相关阅读:
    java:线上问题排查常用手段
    去fastjson笔记
    如何让java中的注释代码执行?
    spring中aop不生效的几种解决办法
    mysql事务隔离级别/脏读/不可重复读/幻读详解
    业务系统-全球化多时区的解决思路
    mysql虚拟列(Generated Columns)及JSON字段类型的使用
    freeswitch笔记(7)-放音控制
    JVM问题典型案例定位学习
    freeswitch笔记(6)-会议功能简介
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1315135.html
Copyright © 2011-2022 走看看