WPF VisualTreeHelper and LogicalTreeHelper and various.

In this page: Understanding the Visual Tree and Logical Tree in WPF , the difference between the Visual Tree and Logical Tree is discussed.

 

Visual Tree and Logical Tree

 

In summary. 

 

Visual Tree

 

 

  • represents all of the elements in your UI which render to an output device (typically, the screen)
  • Used for rendering, event routing, and locating resource (if an element has no logical parent)

 

 

Logical Tree

 

  • Represents the essential structure of your UI. It closely matches the elemtns you declared in XAML, and excludes most visual elements create internally to help render the elements you declared.
  • WPF use logical tree to determine several things including dependency proeprty value inheritence, resource resolution and more.

 

 

How to walk down/up the VisualTree and the Logical Tree

 

with the Help of VisualTreeHelper, you can do 

 

 

  • VisualTreeHelper.GetParent(depObj)
  • VisualTreeHelper.GetChildren(depObj)

 

 

and with the help of LogicalTreeHelper, you can do 

 

 

  • LogicalTreeHelper.GetParent(depObj);
  • LogicalTreeHelper.GetChildren(depObj);

 

Below shows some helper function to help you find a parent of a specific type 

 

 

DependencyObject parent = ExVisualTreeHelper.FindVisualParent<UserControl>(this);

public static class ExVisualTreeHelper
{
    /// <summary>
    /// Finds the visual parent.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sender">The sender.</param>
    /// <returns></returns>
    public static T FindVisualParent<T>(DependencyObject sender) where T : DependencyObject
    {
        if (sender == null)
        {
            return (null);
        }
        else if (VisualTreeHelper.GetParent(sender) is T)
        {
            return (VisualTreeHelper.GetParent(sender) as T);
        }
        else
        {
            DependencyObject parent = VisualTreeHelper.GetParent(sender);
            return (FindVisualParent<T>(parent));
        }
    }
 

 

You can do the same for LogicalTree.

 

 

Remove Visual/Logical Child

You may sometimes require to remove a child from a visual tree or logical tree (actually still investigating when this is justified). 

 

There are two method for the class FrameworkElement.

 

FrameworkElement.RemoveLogicalChild 

FrameworkElement.RemoveVisualChild

 

But they are protected internal method. Generally there are two ways to get around this. 

 

 

  • inherit and override
  • reflection call. 

 

Below show the code to do the reflection call.

 

 

private System.Reflection.MethodInfo GetMethodInfo(string name)
        {
          if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");


          var windowType = typeof(FrameworkElement);
          var methodinfo = windowType.GetMethod(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod);

          if (methodinfo != null)
            return methodinfo;
          return null;
        }

        private void CallRemoveLogicalChild(FrameworkElement window, object child)
        {
          if (window == null) throw new ArgumentNullException("window");
          if (child == null) throw new ArgumentNullException("child");
          var method = GetMethodInfo("RemoveLogicalChild");
          if (method != null)
          {
            method.Invoke(window, new[] { child });
          }

        }

        private void CallRemoveVisualChild(FrameworkElement window, object child)
        {
          if (window == null) throw new ArgumentNullException("window");
          if (child == null) throw new ArgumentNullException("child");

          var method = GetMethodInfo("RemoveVisualChild");
          if (method != null)
          {
            method.Invoke(window, new[] { child });
          }
        }
 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值