zoukankan      html  css  js  c++  java
  • When theme change,the control tree is changed.

    When use   ListView, i want to find it's child :scrollViewer.use code as bellow:

    svALlItems = (ScrollViewer)(VisualTreeHelper.GetChild(lvAllItems, 0) as Border).Child;

    when change theme from areo to non-aero ,for example as classic theme, application crash,the reason at above statement.

    Use VisualTreeHelper to see VisualTree,The method  as bellow:

            string GetVisualTreeInfo(DependencyObject target)
            {
                int i = 0;
                int childCount = 0;
                DependencyObject parentObject = target;
                DependencyObject childObject = null;
                string format = "Rank:{0}    childIndex:{1}   {2}   ";
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("VisualTree:");
                while (true)
                {
                    childCount = VisualTreeHelper.GetChildrenCount(parentObject);
                    if (childCount == 0) break;
    
                    for (int j = 0; j < childCount; j++)
                    {
                        childObject = VisualTreeHelper.GetChild(parentObject, j);
                        stringBuilder.AppendLine(string.Format(format, i, j, childObject));
                    }
    
                    if (childObject == null) break;
                    parentObject = childObject;
                    i++;
                }
                Debug.WriteLine(stringBuilder.ToString());
                return stringBuilder.ToString();
            }
    

    When aero theme,visualTree is :

    Rank:0    childIndex:0   System.Windows.Controls.Border  
    Rank:1    childIndex:0   System.Windows.Controls.ScrollViewer  
    Rank:2    childIndex:0   System.Windows.Controls.Grid  
    Rank:3    childIndex:0   System.Windows.Shapes.Rectangle  
    Rank:3    childIndex:1   System.Windows.Controls.ScrollContentPresenter   
    Rank:3    childIndex:2   System.Windows.Controls.Primitives.ScrollBar Minimum:0 Maximum:0 Value:0   
    Rank:3    childIndex:3   System.Windows.Controls.Primitives.ScrollBar Minimum:0 Maximum:0 Value:0   
    

    When non-aero theme,as classic, visualTree is :

     Rank:0    childIndex:0   Microsoft.Windows.Themes.ClassicBorderDecorator    
    Rank:1    childIndex:0   System.Windows.Controls.ScrollViewer   
    Rank:2    childIndex:0   System.Windows.Controls.Grid   
    Rank:3    childIndex:0   System.Windows.Shapes.Rectangle   
    Rank:3    childIndex:1   System.Windows.Controls.ScrollContentPresenter   
    Rank:3    childIndex:2   System.Windows.Controls.Primitives.ScrollBar Minimum:0 Maximum:0 Value:0   
    Rank:3    childIndex:3   System.Windows.Controls.Primitives.ScrollBar Minimum:0 Maximum:0 Value:0   
    

    the first child is different ,so when we use VisualTreeHelper like this,plase take  care !~

    the above statement should change  as fllow:

     var svALlItems = (ScrollViewer)(VisualTreeHelper.GetChild(listView1, 0) as Decorator).Child;

  • 相关阅读:
    dinic模板
    匈牙利算法(codevs2776)
    线段树(codevs1082)
    KM模板
    kmp模板,线性完成pos
    (一)Python入门-2编程基本概念:03引用的本质-栈内存和堆内存-内存示意图
    (一)Python入门-2编程基本概念:04标识符-帮助系统简单实用-命名规则
    (一)Python入门-2编程基本概念:05变量的声明-初始化-删除变量-垃圾回收机制
    (一)Python入门-2编程基本概念:06链式赋值-系列解包赋值-常量
    (一)Python入门:05Python程序格式-缩进-行注释-段注释
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/1999674.html
Copyright © 2011-2022 走看看