When theme change,the control tree is changed.

ListView主题切换适配
本文介绍了在使用ListView时,如何根据不同主题(如Aero和Classic)正确获取其子元素ScrollViewer的方法。通过使用VisualTreeHelper并注意不同主题下视图树结构的变化,确保应用程序在主题切换时不崩溃。

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;

转载于:https://www.cnblogs.com/xiaokang088/archive/2011/03/30/1999674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值