WPF: Binding to a sequence

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…

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s