Click here to Skip to main content
15,892,809 members
Articles / Desktop Programming / WPF

XAML Debugger Visualizer for WPF

Rate me:
Please Sign up or sign in to vote.
4.93/5 (22 votes)
15 Nov 2007CPOL10 min read 67.7K   1.1K   49  
A debugger visualizer that allows WPF controls and objects to be visualized as XAML.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.IO;
using System.Xml;
using System.Windows.Media;
using System.Windows;
using System.Windows.Documents;

//Apply XamlVisualizer Visualizer to a WPF Visual
[assembly: DebuggerVisualizer(typeof(XamlVisualizer.XamlVisualizer), typeof(XamlVisualizer.XamlVisualizerObjectSource), 
    Target=typeof(Visual), Description = "XAML Visualizer")]

//Apply XamlVisualizer Visualizer to a WPF Style
[assembly: DebuggerVisualizer(typeof(XamlVisualizer.XamlVisualizer), typeof(XamlVisualizer.XamlVisualizerObjectSource),
    Target = typeof(Style), Description = "XAML Visualizer")]

//Apply XamlVisualizer Visualizer to templates
[assembly: DebuggerVisualizer(typeof(XamlVisualizer.XamlVisualizer), typeof(XamlVisualizer.XamlVisualizerObjectSource),
    Target = typeof(FrameworkTemplate), Description = "XAML Visualizer")]

//Apply XamlVisualizer Visualizer to documents
[assembly: DebuggerVisualizer(typeof(XamlVisualizer.XamlVisualizer), typeof(XamlVisualizer.XamlVisualizerObjectSource),
    Target = typeof(FlowDocument), Description = "XAML Visualizer")]

namespace XamlVisualizer
{
    
    /// <summary>
    /// A Visualizer for visualizing WPF types as XAML.  
    /// </summary>
    public class XamlVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            //The stream to which the dat ais written can be obtained by calling GetData
            Stream dataStream = objectProvider.GetData();
            
            //Replace displayForm with your own custom Form or Control.
            using (XamlVisualizerForm displayForm = new XamlVisualizerForm())
            {
                displayForm.XamlText = new StreamReader(dataStream).ReadToEnd();
                displayForm.IsObjectReplaceable = objectProvider.IsObjectReplaceable;
                if (windowService.ShowDialog(displayForm) == DialogResult.OK)
                {
                    //After the dialog is shown the user may click Replace to replace the object
                    MemoryStream replacementDataStream = new MemoryStream();
                    using (XmlWriter xmlWriter = XmlTextWriter.Create(replacementDataStream))
                    {
                        xmlWriter.WriteRaw(displayForm.XamlText);
                    }

                    replacementDataStream.Seek(0, SeekOrigin.Begin);
                    objectProvider.ReplaceData(replacementDataStream);
                }
            }
        }

        // 
        //    XamlVisualizer.TestShowVisualizer(new SomeType());
        // 
        /// <summary>
        /// Tests the visualizer by hosting it outside of the debugger.
        /// </summary>
        /// <param name="objectToVisualize">The object to display in the visualizer.</param>
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(XamlVisualizer));
            visualizerHost.ShowVisualizer();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions