The Current User Mark-up Extension – C# Code

using System;
using System.Security.Principal;
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"
    ///
    ///
    /// 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"
    ///
    /// 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:CurrentUser
    ///
    /// XAML Markup Extension to bind dependency property (DateTime or String)    
    /// to the current date and time (updated each second)    
    /// </summary>
    [MarkupExtensionReturnType(typeof (string))]
    public class CurrentUserExtension : MarkupExtension
    {
        #region State

        #endregion

        #region Constructors

        #endregion

        /// <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)
        {
            try
            {
                return WindowsIdentity.GetCurrent().Name.Split('\\')[1];
            }
            catch (NullReferenceException)
            {
                return string.Empty;
            }
        }
    }
}

One Response to “The Current User Mark-up Extension – C# Code”

  1. […] simple mark-up extension that displays the name of current user in your XAML. The code can be found here, and the XAML here. Below is an example of the Current User mark-up extension in […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: