Find inner visual element


   
   

<Window x:Class="VisualTree.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="VisualTree" Height="300" Width="300">
  
    <Grid>
      <Button Click="MyClickEvent" Name="btnGo">
        <TextBox Width="173" Height="27" Name="txt">
        </TextBox>        
      </Button>      
    </Grid>  
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace VisualTree
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        void MyClickEvent(object sender, RoutedEventArgs e)
        {
            object desiredNode = btnGo.FindName("txt");
            if (desiredNode is TextBox)
            {
                TextBox desiredChild = desiredNode as TextBox;
                desiredChild.Background = Brushes.Green;
            }
        }

    }
}

   
    
    
     


Point Hit Test with VisualTreeHelper.HitTest


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="InputExamples.PointHitTest" 
  Width="640" Height="480">

  <StackPanel.Resources>
    <Storyboard x:Key="OnLoaded"/>
  </StackPanel.Resources>
  <StackPanel.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
    </EventTrigger>
  </StackPanel.Triggers>
  <Rectangle Fill="Red" Margin="20" Width="200" Height="200" x:Name="RectangleArea"/>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace InputExamples
{
  public partial class PointHitTest
  {
    public PointHitTest()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      CompositionTarget.Rendering += this.CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        Point position = Mouse.GetPosition(RectangleArea);
      VisualTreeHelper.HitTest(RectangleArea,null,new HitTestResultCallback(HitTestResultHandler),
              new PointHitTestParameters(position)
      );
    }
    
    public HitTestResultBehavior HitTestResultHandler(HitTestResult result)
    {
      PointHitTestResult hitResult = (PointHitTestResult)result;
      Console.WriteLine(((FrameworkElement)hitResult.VisualHit).Name);
      Console.WriteLine(hitResult.PointHit.ToString());
         return HitTestResultBehavior.Continue;
    }
  }
}

   
    
    
     


About Dialog with Tree Walking







1
2
3
4
5
6
7
8
9






text


//File:Window.xaml.cs

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

public partial class AboutDialog : Window
{
public AboutDialog()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
PrintVisualTree(this);
}
void PrintVisualTree(DependencyObject obj)
{
Debug.WriteLine(obj);
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { PrintVisualTree(VisualTreeHelper.GetChild(obj,i)); } } } [/csharp]

Logical Visual Tree Sample


   
   

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LogicalVisualTreeSample" Height="300" Width="300">
  <WrapPanel Name="rootPanel">
    <Button>_Click me</Button>
  </WrapPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      DumpLogicalTree(rootPanel);
    }
    protected override void OnContentRendered(EventArgs e) {
      base.OnContentRendered(e);
      DumpVisualTree(rootPanel);
    }

    void DumpLogicalTree(object parent) {
      string typeName = parent.GetType().Name;
      string name = null;
      DependencyObject doParent = parent as DependencyObject;

      if( doParent != null ) {
        name = (string)(doParent.GetValue(FrameworkElement.NameProperty) ?? "");
      }
      else {
        name = parent.ToString();
      }

      Debug.WriteLine(typeName);
      Console.WriteLine(name);
      if( doParent == null ) { return; }

      foreach( object child in LogicalTreeHelper.GetChildren(doParent) ) {
        DumpLogicalTree(child);
      }
    }

    void DumpVisualTree(DependencyObject parent) {
      string typeName = parent.GetType().Name;
      string name = (string)(parent.GetValue(FrameworkElement.NameProperty) ?? "");
      
      Debug.WriteLine(typeName);
      Console.WriteLine(name);

      for( int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i ) {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        DumpVisualTree(child);
      }
    }

  }
}

   
    
    
     


Find enclosure component


   
   

<Window x:Class="VisualTree.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="VisualTree" Height="300" Width="300">
  <Grid>      
      <Button Click="MyClickEvent" Name="btnGo">
        <TextBox Width="173" Height="27" Name="txt">
        </TextBox>        
      </Button>      
    </Grid>  
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace VisualTree
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void MyClickEvent(object sender, RoutedEventArgs e)
        {
            object desiredNode = btnGo.FindName("txt");
            if (desiredNode is TextBox)
            {
                TextBox desiredChild = desiredNode as TextBox;
                desiredChild.Background = Brushes.Green;
            }
        }

    }
}

   
    
    
     


Print Visual Tree










//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections;

namespace WPFTrees
{

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
}

public void PrintVisualTree(Visual visual)
{
Console.WriteLine(visual.GetType().ToString()) ;

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) { PrintVisualTree((Visual)VisualTreeHelper.GetChild(visual, i)); } } private void buttonPrint_Click(Object sender, RoutedEventArgs e) { PrintVisualTree(this.button1); } } } [/csharp]

Print Logical Tree


   
   
<Window x:Class="WPFTrees.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="WPFTrees" Height="304" Width="702" Name="MyWindow">
  <DockPanel Name="DockPanel1">
    <StackPanel DockPanel.Dock="Left" Margin="6,4,4,4" >
      <Canvas Name="canvas1">
        <Button Name="button1">
          <Ellipse Name="ellipse1" Height="100" Width="100" Fill="Red"></Ellipse>
        </Button>
      </Canvas>
    </StackPanel>
    <Button DockPanel.Dock="Left" Height="20" Width="100" 
      HorizontalAlignment="Left" Margin="1,4,4,4" Click="buttonPrint_Click">Print Trees</Button>
  </DockPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections;

namespace WPFTrees
{

  public partial class Window1 : System.Windows.Window
  {

    public Window1()
    {
      InitializeComponent();
    }
    public void PrintLogicalTree(Object obj)
    {
      if (obj is FrameworkElement)
      {
        FrameworkElement element = (FrameworkElement)obj;
        Console.WriteLine(element.GetType().ToString());

        IEnumerable children = LogicalTreeHelper.GetChildren(element);
        foreach (Object child in children)
        {
          PrintLogicalTree(child);
        }
      }
    }

    private void buttonPrint_Click(Object sender, RoutedEventArgs e)
    {
      PrintLogicalTree(this.button1);
    }

  }

}