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…

Sorry, this blog does not support comments.

I used various blog hosting services since this blog was established in 2005, but unfortunately they turned out to be unreliable in the long term and comment threads were lost in unavoidable transitions. At this time I don't want to enable third-party services for comments since it has become obvious in recent years that these providers invariably monetize information about their visitors and users.

Please use the links in the page footer to get in touch with me. I'm available for conversations on Keybase, Matrix, Mastodon or Twitter, as well as via email.