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

     

  • 相关阅读:
    使用 Anthem.NET 的经验小结
    使用 Anthem.NET 的常见回调(Callback)处理方式小结
    Anthem.NET 的回调流程图
    客户端调用服务器端方法——ASP.NET AJAX(Atlas)、Anthem.NET和Ajax.NET Professional实现之小小比较
    ASP.NET AJAX(Atlas)和Anthem.NET——管中窥豹般小小比较
    使用Anthem.NET 1.5中的FileUpload控件实现Ajax方式的文件上传
    Access中一句查询代码实现Excel数据导入导出
    Access中复制表
    tensorflow kmeans 聚类
    tensorflow knn mnist
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1315135.html
Copyright © 2011-2022 走看看