Please click this logo to help me get on their beta program:

Xobni outlook add-in for your inbox








31/3/2007

WPF: IntSequence helper class updated, including dependency properties

Filed under: General — Oliver Sturm @ 9:57 pm - 1 year, 8 months ago

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 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Powered by WordPress
© Copyright 2005-2008 Oliver Sturm