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:

    <engine:Grid x:Key="gameGrid" />
    <helpers:IntSequence x:Key="rowDummyList" EndVal="{Binding Source={StaticResource gameGrid}, Path=RowCount}" />

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;

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s