WPF: IntSequence Helper Class Updated, Including Dependency Properties

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

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.