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

     

  • 相关阅读:
    paip.提升安全性时间判断
    paip.提升安全性本机硬件绑定
    paip.提升性能服务器环境及编程语言架构选择
    paip.提升用户体验提示语
    paip.提升效率输入框不能粘贴的问题
    paip.银联支付接口订单号uuid算法
    paip.Net Framework各个版本的功能不同总结
    Answer 3.0 .NET开源网站功能API说明
    Paip.YXSHOP易想商场功能模块说明
    paip.提升效率文件搜索工具总结
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1315135.html
Copyright © 2011-2022 走看看