zoukankan      html  css  js  c++  java
  • WPF常用代码:Visual Logical Tree

    1. 自定义路由事件
      // Register the routed event
      public static readonly RoutedEvent SelectedEvent = 
          EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble, 
          typeof(RoutedEventHandler), typeof(MyCustomControl));
       
      // .NET wrapper
      public event RoutedEventHandler Selected
      {
          add { AddHandler(SelectedEvent, value); } 
          remove { RemoveHandler(SelectedEvent, value); }
      }
      
      // Raise the routed event "selected"
      RaiseEvent(new RoutedEventArgs(MyCustomControl.SelectedEvent));
    2. 查找特定的Parent元素或Child元素
       // walk up the visual tree to find object of type T, starting from initial object
              public static T FindUpVisualTree<T>(DependencyObject initial) where T : DependencyObject
              {
                  DependencyObject current = initial;
      
                  while (current != null && current.GetType() != typeof(T))
                  {
                      current = VisualTreeHelper.GetParent(current);
                  }
                  return current as T;
              }
      
              public static T FindDownVisualTree<T>(Visual parent) where T : Visual
              {
                  T child = default(T);
                  int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                  for (int i = 0; i < numVisuals; i++)
                  {
                      Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                      child = v as T;
                      if (child == null)
                      {
                          child = FindDownVisualTree<T>(v);
                      }
                      if (child != null)
                      {
                          break;
                      }
                  }
                  return child;
              }
  • 相关阅读:
    JavaScript——引用类型
    react+express+mongodb搭建个人博客
    JavaScript——变量及其作用域
    CSS——盒子模型
    CSS——浮动及清除浮动
    hexo博客分支教训
    使用Node.js+Express 简易开发服务端实例
    发布Nuget包命令
    当心引用类型的“坑”
    sqlcmd相关
  • 原文地址:https://www.cnblogs.com/maigc249/p/5130893.html
Copyright © 2011-2022 走看看