Click here to Skip to main content
15,892,199 members
Articles / Desktop Programming / WPF

Attaching a Virtual Branch to the Logical Tree in WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (33 votes)
6 May 2007CPOL8 min read 253.9K   1.2K   62  
Reviews a pattern which enables new possibilities for data binding.
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;

namespace ValidationRuleWithBoundProperty
{
	/// <summary>
	/// Ensures that the validated value is a multiple of a specified number.
	/// </summary>
	public class IsMultipleOfValidationRule : ValidationRule
	{
		private IntegerContainer divisorContainer;

		public IntegerContainer DivisorContainer
		{
			get { return divisorContainer; }
			set { divisorContainer = value; }
		}

		public override ValidationResult Validate( object value, CultureInfo cultureInfo )
		{
			try
			{
				int dividend = (int)Convert.ChangeType( value, typeof( int ) );

				int divisor = this.DivisorContainer.Value;

				if( dividend % divisor == 0 )
					return ValidationResult.ValidResult;

				return new ValidationResult(
					false,
					"The number is not a multiple of " + divisor );
			}
			catch
			{
				return new ValidationResult( false, "Not a number." );
			}
		}
	}
}

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)


Written By
Software Developer (Senior)
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[^].

Comments and Discussions