0% found this document useful (0 votes)
57 views13 pages

How To Walk Through and Print The Logical and Visual Trees

This document provides instructions for creating a WPF project that displays the logical and visual trees of a button's control when clicked. It involves: 1. Creating a project with a button and adding a click event handler. 2. Adding code to the handler to print the logical and visual trees to the console. 3. Creating a class to allocate a console window for the output, since WPF apps normally hide it. 4. Running the program - clicking the button now displays the tree structures in the console.

Uploaded by

umersabah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views13 pages

How To Walk Through and Print The Logical and Visual Trees

This document provides instructions for creating a WPF project that displays the logical and visual trees of a button's control when clicked. It involves: 1. Creating a project with a button and adding a click event handler. 2. Adding code to the handler to print the logical and visual trees to the console. 3. Creating a class to allocate a console window for the output, since WPF apps normally hide it. 4. Running the program - clicking the button now displays the tree structures in the console.

Uploaded by

umersabah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1. Create a new WPF Project “LogicalAndVisualTree”.

2. Add a button in Designer view.


3. Add event to a button
 Click on this
 Add event “OnButtonClick” against “Click”
 Then press “Enter”
4. After pressing Enter, IDE will automatically take control to this page
“MainWindow.xaml.cs” and a new function will be created with the name “OnButtonClick”.
This is the function where you can add functionality/events to your button. As the requirement of
our tutorial is to Print Logical and Physical Tree when we click button, so for this add the
following code lines in the highlighted portion:
Console.WriteLine("Logical Tree:");
PrintLogicalTree(0, this);
Console.WriteLine();
Console.WriteLine("Visual Tree:");
PrintVisualTree(0, this);
Console.ReadLine();
5. Add the following code after the “OnButtonClick” function. These functions walk through
and print Logical and Physical trees.

void PrintLogicalTree(int depth, object obj)


{
// Print the object with preceding spaces that represent its depth
Console.WriteLine(new string(' ', depth) + obj);
// Sometimes leaf nodes aren’t DependencyObjects (e.g. strings)
if (!(obj is DependencyObject)) return;
// Recursive call for each logical child
foreach (object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
PrintLogicalTree(depth + 1, child);
}

void PrintVisualTree(int depth, DependencyObject obj)


{
// Print the object with preceding spaces that represent its depth
Console.WriteLine(new string(' ', depth) + obj);
// Recursive call for each visual child
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
PrintVisualTree(depth + 1, VisualTreeHelper.GetChild(obj, i));
}
6. By default, WPF applications do not show the Console Output, so for this Add a new Class
to the project by following the shown process.
7. Now copy paste the following code in the newly created file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LogicalAndVisualTree
{
public class ConsoleHelper
{
/// <summary>
/// Allocates a new console for current process.
/// </summary>
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();

/// <summary>
/// Frees the console.
/// </summary>
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
}

}
8. Now add the following code lines in the specified positions:

ConsoleHelper.AllocConsole();

ConsoleHelper.FreeConsole();
9. Now Press “F5” to execute the Program. Click on Button, it will show the Console Output,
displaying the Logical and Physical Trees.
In the same way, you can view Logical and Physical Tree of any control.

You might also like