Formatted Digital Clock


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:src="clr-namespace:MyNameSpace.FormattedDigitalClock"
        Title="Formatted Digital Clock">
    <Window.Resources>
        <src:ClockTicker x:Key="clock" />
        <src:FormattedTextConverter x:Key="conv" />
    </Window.Resources>
    <Window.Content>
        <Binding Source="{StaticResource clock}" Path="DateTime" 
                 Converter="{StaticResource conv}" 
                 ConverterParameter="... {0:T} ..." />
    </Window.Content>
</Window>
//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using System.Globalization;
using System.Windows.Data;

namespace MyNameSpace.FormattedDigitalClock
{
    public class ClockTicker : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public DateTime DateTime
        {
            get { return DateTime.Now; }
        }
        public ClockTicker()
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += TimerOnTick;
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Start();
        }

        void TimerOnTick(object sender, EventArgs args)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs("DateTime"));
        }
    }
    public class FormattedTextConverter : IValueConverter{
        public object Convert(object value, Type typeTarget,object param, CultureInfo culture)
        {
            if (param is string)
                return String.Format(param as string, value);

            return value.ToString();
        }
        public object ConvertBack(object value, Type typeTarget,object param, CultureInfo culture)
        {
            return null;
        }
    }
}