Click here to Skip to main content
13,148,136 members (41,458 online)
Click here to Skip to main content

Stats

320.6K views
4.3K downloads
263 bookmarked
Posted 11 Nov 2006

WPF.JoshSmith

, 13 Jul 2008
A free library of controls and utility classes for use in WPF applications.
WPF.JoshSmith
Resources
Images
Composers
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
Robots
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
Test.CenteredContentControl
harpsichord.jpg
Properties
Test.DragCanvas
Images
girls.gif
Properties
Test.ListViewDragDropManager
Properties
Test.RegexValidator
Properties
Test.SlidingListBox
Properties
Test.SmartTextBox
Properties
Test.UnloadedManager
Properties
TestPages
Test.ValueConverterGroup
Properties
WPF.JoshSmith
Adorners
Controls
Utilities
Validation
Data
ValueConverters
Input
Markup
Panels
Properties
ServiceProviders
UI
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
harpsichord.jpg
girls.gif
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
harpsichord.jpg
girls.gif
// Copyright (C) Josh Smith - July 2006
using System;
using System.Globalization;
using System.Windows.Data;
using System.Xml;

namespace WPF.JoshSmith.Data.ValueConverters
{
    /// <summary>
    /// This value converter creates a .NET object from the XAML contained in an XmlElement.  The object
    /// created can be used as the content of a WPF control or ui element, such as the ContentPresenter.
    /// The inner xml of the XmlElement passed to the converter must contain valid XAML.
    /// </summary>
    [ValueConversion(typeof(XmlElement), typeof(object))]
    public class XamlToObjectConverter : IValueConverter
    {
        #region Data

        // Every call to the XamlReader requires a ParserContext, so a static instance is kept
        // to reduce the overhead of creating one every time a value is converted.
        private static System.Windows.Markup.ParserContext parserContext;

        #endregion // Data

        #region Static Constructor

        static XamlToObjectConverter()
        {
            // Initialize the parser context, which provides xml namespace mappings used when
            // the loose XAML is loaded and converted into a .NET object.
            parserContext = new System.Windows.Markup.ParserContext();
            parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        }

        #endregion // Static Constructor

        #region Convert

        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // The 'value' parameter must be an XmlElement.
            XmlElement elem = value as XmlElement;

            // We need to create a MemoryStream because the XamlReader requires a stream
            // from which the XAML is read.  
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                // Convert the inner xml of the element into a byte array so
                // that it can be loaded into the memory stream.
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(elem.InnerXml);

                // Write the XAML element into the memory stream.
                stream.Write(bytes, 0, bytes.Length);

                // Reset the stream's current position back to the beginning so that when it
                // is read from, the reading will begin at the correct place.
                stream.Position = 0;

                // This is the magic method call which converts XAML into a .NET object.
                return System.Windows.Markup.XamlReader.Load(stream, parserContext);
            }
        }

        #endregion // Convert

        #region ConvertBack

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("ConvertBack not supported.");
        }

        #endregion // ConvertBack
    }
}

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)

Share

About the Author

Josh Smith
Software Developer (Senior) Black Pixel
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.1 | Last Updated 13 Jul 2008
Article Copyright 2006 by Josh Smith
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid