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();}
     
    }
     
    }
     

     

  • 相关阅读:
    String系列
    java初始化构造函数调用顺序
    转发和重定向的区别
    HttpServletResponse对象
    JSP九大隐式对象
    关于异常
    MySQL下载、安装及启动
    MySQL的启动
    MySQL下载及安装
    U盘安装Win7操作系统
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1315135.html
Copyright © 2011-2022 走看看