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;
  }
}