Machine Name Markup Extension

I was looking for a simple way to display the machine name in a WPF application I was writing. I considered using a POCO with a static method that returned the machine name from System.Environment but that then meant binding the value n some way over a Dependency Property or one implementing INotifyProprtyChanged. Thinking a little more on the problem I remembered reading about Markup Extensions in WPF. So I thought that might be a simple way of providing the machine name using this WPF feature. Boy, was I surprised on how simple it really was! The following code was all I needed to get this matter finished:

using System;
using System.Windows.Markup;
namespace i4.WPF.Custom.MarkupExtensions
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use these Markup Extensions in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace=”clr-namespace:i4.WPF.Custom.MarkupExtensions”
    /// or
    ///     xmlns:MyNamespace=”http://www.intelligence4.net/WPF/Custom/MarkupExtensionsssdk/2009/xaml&#8221;
    ///
    ///
    /// Step 1b) Using these Markup Extensions in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace=”clr-namespace:i4.WPF.Custom.MarkupExtensions;assembly=i4MarkupExtensions”
    /// or
    ///     xmlns:MyNamespace=”http://www.intelligence4.net/WPF/Custom/MarkupExtensionsssdk/2009/xaml&#8221;
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     “Add Reference”->”Projects”->[Select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your Markup Extensions in the XAML file.
    ///
    ///     MyNamespace:MachineName
    /// 
    /// </summary>
    [MarkupExtensionReturnType(typeof (string))]
    public class MachineNameExtension : MarkupExtension
    {
        /// <summary>        
        /// Overrides MarkupExtension.ProvideValue to return         
        /// the current datetime and hook a timer to update the         
        /// target propery each second         
        /// (using DispatcherTime with Background priority)        
        /// </summary>        
        /// <param name=”serviceProvider”></param>        
        /// <returns></returns>        
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return Environment.MachineName;
        }
    }
}

 

Using the Markup Extension in code is as simple as:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        
        xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
        xmlns:d=http://schemas.microsoft.com/expression/blend/2008
        xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
        xmlns:MyNamespace="clr-namespace:i4.WPF.Custom.MarkupExtensions;assembly=i4MarkupExtensions"
        mc:Ignorable="d"
        x:Class="MarkupExtensionsTest.Window1"
        Title="Window1"
        Height="81"
        Width="300" 
        Background="#FFB5B3E0">
    <Grid>
        <TextBox Margin="8" TextWrapping="Wrap" Background="#FFF4D18D">
            <TextBox.Text>
                <MyNamespace:MachineName />
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

and the output from running this program is:

That’s all folks…

Advertisements

~ by Intelligence4 on December 29, 2010.

 
%d bloggers like this: