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

Xobni outlook add-in for your inbox








31/3/2007

WPF: Binding to a sequence

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

In certain contexts I have always found it useful to bind to a sequence of numbers, or sometimes even just a dummy collection with a certain number of elements. It’s like a for-loop, just for data binding. I’ve found three good ways of doing that in XAML, as the following two source code snippets show:

<Window x:Class="WPFSequenceBinding.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:sys="clr-namespace:System;assembly=mscorlib"
	xmlns:local="clr-namespace:WPFSequenceBinding"
	Title="WPFSequenceBinding" Height="300" Width="300">
	<Window.Resources>
		<x:Array x:Key="list1" Type="{x:Type sys:Int32}">
			<sys:Int32>1</sys:Int32>
			<sys:Int32>2</sys:Int32>
			<sys:Int32>3</sys:Int32>
			<sys:Int32>4</sys:Int32>
			<sys:Int32>5</sys:Int32>
		</x:Array>
		<ObjectDataProvider x:Key="list2" ObjectType="{x:Type sys:Array}" MethodName="CreateInstance">
			<ObjectDataProvider.MethodParameters>
				<sys:Type>sys:Int32</sys:Type>
				<sys:Int32>5</sys:Int32>
			</ObjectDataProvider.MethodParameters>
		</ObjectDataProvider>
		<local:IntSequence x:Key="list3" StartVal="1" EndVal="5" />
	</Window.Resources>
	<StackPanel>
		<ListBox ItemsSource="{Binding Source={StaticResource list1}}" />
		<ListBox ItemsSource="{Binding Source={StaticResource list2}}" />
		<ListBox ItemsSource="{Binding Source={StaticResource list3}}" />
	</StackPanel>
</Window>
public class IntSequence : IEnumerable<int> {
	private int startVal = 1;
	public int StartVal {
		get { return startVal; }
		set { startVal = value; }
	} 
	
	private int endVal = 10;
	public int EndVal {
		get { return endVal; }
		set { endVal = vlue; }
	} 
	
	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;
	}
}

All these approaches come with their own advantages and disadvantages. The x:Array block can be defined completely in XAML and is obviously not restricted to sequential values. Then again, for a simple sequence, there’s a lot of typing and/or copying/pasting to be done and the result is extremely verbose. The ObjectDataProvider with the dynamically created array is an interesting approach, but the array is not actually initialized with values (so it contains a bunch of zeros) and the XAML code is also quite verbose. Finally the IntSequence looks great in XAML, but it requires a helper class, however simple. Well, there’s something for everyone here…

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