Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / WPF

Immerse Yourself in WPF: A "World Clocks" Application as Literate Code

Rate me:
Please Sign up or sign in to vote.
4.88/5 (39 votes)
16 Aug 200713 min read 114.9K   5K   111  
Literate code describing how to build a simple "world clocks" application in WPF
/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BinaryComponents.WorldClocks.Windows
{
	/// <summary>
	/// Interaction logic for ChooseTimeZoneWindow.xaml
	/// </summary>

	public partial class ChooseTimeZoneWindow : Window
	{
		public ChooseTimeZoneWindow()
		{
			InitializeComponent();
		}

		public Data.TimeInfo Add( Window owner )
		{
			Owner = owner;
			ShowDialog();

			if( DialogResult.Value )
			{
				TimeZoneInfo tzi = (TimeZoneInfo) _tzCombo.SelectedItem;

				Data.TimeInfo ti = new Data.TimeInfo( tzi );

				if( _descriptionControl.Text.Trim() != string.Empty )
				{
					ti.DisplayName = _descriptionControl.Text.Trim();
				}

				return ti;
			}
			else
			{
				return null;
			}
		}

		public bool Edit( Window owner, Data.TimeInfo ti )
		{
			_tzCombo.SelectedItem = ti.TimeZoneInfo;
			_tzCombo.IsEnabled = false;

			if( ti.IsDisplayNameOverridden )
			{
				_descriptionControl.Text = ti.DisplayName;
			}
			
			Owner = owner;
			ShowDialog();

			if( DialogResult.Value )
			{
				if( _descriptionControl.Text.Trim() != string.Empty )
				{
					ti.DisplayName = _descriptionControl.Text.Trim();
				}

				return true;
			}
			else
			{
				return false;
			}
		}

		protected override void OnInitialized( EventArgs e )
		{
			base.OnInitialized( e );

			var timezones = TimeZoneInfo.GetSystemTimeZones();

			_tzCombo.ItemsSource = timezones;
		}

		private void _okButton_CanExecute( object sender, CanExecuteRoutedEventArgs e )
		{
			e.CanExecute = _tzCombo != null && _tzCombo.SelectedItem != null;
		}

		private void _okButton_OnExecuted( object sender, ExecutedRoutedEventArgs e )
		{
			DialogResult = true;
		}

		private void _tzCombo_SelectionChanged( object sender, SelectionChangedEventArgs e )
		{
			_timeZoneMap.TimeZoneInfo = (TimeZoneInfo) _tzCombo.SelectedItem;
		}

		public readonly static RoutedUICommand Ok = new RoutedUICommand();
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
I'm currently working for a small start-up company, BinaryComponents Ltd, producing the FeedGhost RSS reader.

FeedGhost RSS Reader:
http://www.feedghost.com

Bespoke Software Development
http://www.binarycomponents.com

Comments and Discussions