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

     

  • 相关阅读:
    计数排序【代码】
    快速排序【代码】
    基于最大堆实现最大优先队列
    Spring入门(1)
    AJAX初步理解
    选择器
    Hibernate的映射
    Hibernate配置(2)
    查看mysql的安装路径
    Hibernate入门(1)
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1315135.html
Copyright © 2011-2022 走看看