Painlessly debugging the WPF Visual Tree in a MVVM-heavy app

By Tamir Dresher

Visual Studio comes with a built-in Visual Tree Visualizer which is really useful, but in order to start using it you have to find a reference to the instance of the FrameworkElement (i.e. UserControl, Window etc.) that you want to visualize.

Getting the reference to the desired FrameworkElement is especially hard if you are using proper MVVM architecture style, since most (if not all) of your code is located in the ViewModel and the ViewModel doesn’t have a reference to the View (i.e. the FrameworkElement).  When you hit a breakpoint inside your ViewModel, you have no easy way to get to the instance of your View.

OzCode comes with a great feature called Show All Instances that makes this super easy!

Suppose we have the following View which displays a list of Students:

Our XAML file is very simple, it consists of a button which is docked to the top of the view and ListView that is bound to a property called Students, the ItemTemplate for a Student consists of 3 TextBlocks for the titles and 3 TextBlocks for the values:

<UserControl x:Class="FindTheVisualTree.StudentsRecordsView"
  <DockPanel LastChildFill="True">
    <Button DockPanel.Dock="Top" Command="{Binding FindTheTreeCommand}">Find The Visual Tree</Button>
    <ListView ItemsSource="{Binding Students}">
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />

                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />

              <TextBlock Text="Id:" />
              <TextBlock Grid.Column="1"
                     Text="{Binding Id}" />
              <TextBlock Grid.Row="1"
                     Text="Name:" />
              <TextBlock Grid.Row="1"
                     Text="{Binding Name}" />
              <TextBlock Grid.Row="2"
                     Text="Phone:" />
              <TextBlock Grid.Row="2"
                     Text="{Binding Phone}" />


Our ViewModel is also very simple:

public class StudentsRecordsViewModel
    private DelegateCommand _findTheTreeCommand;

    public StudentsRecordsViewModel()
        var students = new List<Student>();
        foreach (var id in Enumerable.Range(1,30))
            students.Add(new Student()
                Id = id,
                Name = "Student "+id,
                Phone = string.Concat(Enumerable.Repeat(id.ToString(),9))
        Students = students;

    public IEnumerable<Student> Students { get; set; }

    public ICommand FindTheTreeCommand
            return _findTheTreeCommand ?? (_findTheTreeCommand = new DelegateCommand(() =>


In this example I’m creating 30 students when the ViewModel is created. I’ve added a command that is bound to the button on the View which will trigger a break when the button is clicked:

While we are in the breakpoint, we wish to explore the Visual Tree. You can see that neither Autos nor Locals windows give any reference to the StudentsRecordsView.

To get a reference to the View we’ll use OzCode –  Show All Instances of Type:

In the “Show All Instances” window I’ll write the name of the type we’re interested in – StudentsRecordsView, and click the Show Instances button:

OzCode found the instance I’m looking for. Now we can open the WPF Tree Visualizer by clicking on the magnifying glass next to the type name:

Now we can investigate the View, see what the tree structure is, what the values of the properties inside the controls are, and even see a rendering of the UI control, at any level in the Visual Tree.

That’s it! Happy investigating!

About the author: Tamir Dresher is a Senior Consultant at CodeValue and a Lecturer on Software Engineering at Ruppin Academic Center.

Tamir specializes in .NET Technologies such as WPF, WCF, Azure, Windows Phone 8 and Windows 8 Applications, and has a lot of experience in Software Architecture and Distributed Systems.




Keep your code
in good shape!

Follow the latest news in the .NET debugging industry

Ready to Dive into Your Prod Code?

Easy debugging with full time-travel data

Share on facebook
Share on twitter

Recent Posts

Follow Us

Join OzCode YouTube Channel

Let’s start debugging, it’s free!

Keep your code in good shape!

Follow the latest news in the .NET debugging industry
Ozcode Logo

This website uses cookies to ensure you get the best experience on our website.