WPF遍历视觉树与逻辑树

xaml代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded_1">
    <Grid>
        <Button Content="button" Margin="179,131,206,150"></Button>
    </Grid>
</Window>

后台cs代码:

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            StringBuilder tree = new StringBuilder();
            PrintVisualTree(this, 0, tree);
            Console.WriteLine("VisualTree:");
            Console.WriteLine(tree.ToString());

            tree.Clear();
            PrintLogicalTree(this, 0, tree);
            Console.WriteLine("LogicalTree:");
            Console.WriteLine(tree.ToString());
        }

        public void PrintVisualTree(DependencyObject obj, int level, StringBuilder tree)
        {
            tree.AppendLine(new string(' ', level) + obj);
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                PrintVisualTree(VisualTreeHelper.GetChild(obj, i), level + 1, tree);
            }
        }

        public void PrintLogicalTree(DependencyObject obj, int level, StringBuilder tree)
        {
            tree.AppendLine(new string(' ', level) + obj);
            foreach (var v in LogicalTreeHelper.GetChildren(obj))
            {
                if (v is DependencyObject)
                {
                    PrintLogicalTree(v as DependencyObject, level + 1, tree);
                }
            }
        }

//输出
VisualTree:
WpfApplication1.MainWindow
 System.Windows.Controls.Border
  System.Windows.Documents.AdornerDecorator
   System.Windows.Controls.ContentPresenter
    System.Windows.Controls.Grid
     System.Windows.Controls.Button: button
      Microsoft.Windows.Themes.ButtonChrome
       System.Windows.Controls.ContentPresenter
        System.Windows.Controls.TextBlock
   System.Windows.Documents.AdornerLayer

LogicalTree:
WpfApplication1.MainWindow
 System.Windows.Controls.Grid
  System.Windows.Controls.Button: button

从视觉树的结构中可以看到,Button的Content是显示在TextBlock上的,所以,我们可以通过查找视觉树来修改Button的Content样式。

        private void buttonSunny_Click_1(object sender, RoutedEventArgs e)
        {
            ChangeVisualTree<TextBlock>(this);
            
        }

        public void ChangeVisualTree<T>(DependencyObject obj) where T : FrameworkElement
        {
            if (obj is T)
            {
                switch (typeof(T).Name)
                {
                    case "TextBlock":
                        TextBlock textBlock = obj as TextBlock;
                        textBlock.Background = Brushes.Yellow;
                        textBlock.Foreground = Brushes.Brown;
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                ChangeVisualTree<T>(VisualTreeHelper.GetChild(obj, i));
            }
        }

通过 WPF Inspector可以更加详细地了解WPF应用的树形结构和样式等,也可以修改样式,用法很简单,先运行WPF应用,然后打开WPF Inspector,等其加载完成之后,双击列表中的应用即可。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值