« MVPs.Add(this);WPF: Binding to a sequence »

WPF: IntSequence helper class updated, including dependency properties

31/03/07

Permalink 09:57:50 pm
Categories: General

WPF: IntSequence helper class updated, including dependency properties

This struck me right after my recent post about binding to arbitrary sequences: my helper class was implemented with traditional .NET properties, which isn't optimal for use with WPF. One thing specifically isn't good for my purpose, which is the fact that a "normal" property can't be the target of WPF data binding. Like in this case:

<Window.Resources>
	<engine:Grid x:Key="gameGrid" />
	<helpers:IntSequence x:Key="rowDummyList" EndVal="{Binding Source={StaticResource gameGrid}, Path=RowCount}" />
</Window.Resources>

In this case I'm trying to bind the EndVal of the sequence to a value obtained from a different object, and that didn't work using the standard properties. So I have created an updated version of the IntSequence class, which uses dependency properties to make this kind of binding possible. Here it is:

public class IntSequence : DependencyObject, IEnumerable<int> {
	public static readonly DependencyProperty StartValProperty;
	public static readonly DependencyProperty EndValProperty;

	static IntSequence( ) {
		StartValProperty = DependencyProperty.Register("StartVal",
			typeof(int), typeof(IntSequence), new PropertyMetadata(1));
		EndValProperty = DependencyProperty.Register("EndVal",
			typeof(int), typeof(IntSequence), new PropertyMetadata(10));
	}

	public int StartVal {
		get { return (int) GetValue(StartValProperty); }
		set { SetValue(StartValProperty, value); }
	}

	public int EndVal {
		get { return (int) GetValue(EndValProperty); }
		set { SetValue(EndValProperty, value); }
	}

	IEnumerator<int> IEnumerable<int>.GetEnumerator( ) {
		for (int val = StartVal; val <= EndVal; val++)
			yield return val;
	}

	IEnumerator IEnumerable.GetEnumerator( ) {
		foreach (int item in this)
			yield return item;
	}
}

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Please complete the song title below. Hint: enter 'h', 'e', 'a', 'v', 'e', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
March 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31