« Give me the maximum and no more ListBox! | Main | New Windows Live Writer Beta available »

Range converter for WPF

A member on the MSDN forums asked how to enable a button based on the index of the currently selected item in a ListBox. If the index was 0, they wanted the Button to be disabled. I whipped up a Converter:

 

  [ValueConversion(typeof(int), typeof(bool))]
  public class IntToBooleanRangeConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, 
object parameter, System.Globalization.CultureInfo culture) { string range = parameter as string; int intVal = (int) value; if (!string.IsNullOrEmpty(range)) { string[] nums = range.Split(new char[] { '-' }); int[] checkVal = new int[nums.Length]; for(int i = 0; i < nums.Length; i ++) { int.TryParse(nums[i], out checkVal[i]); }

if (nums.Length == 1)
{
return checkVal[0] == intVal;
}
else if (nums.Length == 2)
{
// is it #-# or #-?
if (!string.IsNullOrEmpty(nums[1]))
{
// # - #
return (intVal >= checkVal[0] && intVal <= checkVal[1]);
}
else
{
// #-
return intVal >= checkVal[0];
}
}
else
{
throw new ArgumentOutOfRangeException("IntToBooleanRangeConverter:
Range must be in format #-#, #-, or #"
);
}
}
throw new ArgumentOutOfRangeException("IntToBooleanRangeConverter:
Range must be in format #-#, #-, or #"
);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}

}


Here's a demo:


<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:WiredPrairieNamespace"
   
xmlns:System="clr-namespace:System;assembly=mscorlib"
   x:Class="WiredPrairieNamespace.Window1"
   x:Name="Window"
   Title="Window1"
   Width="640" Height="480">

    <
Window.Resources>
    <
local:IntToBooleanRangeConverter x:Key="IntToBooleanRangeConverter" />
    </
Window.Resources>

    <
Grid x:Name="LayoutRoot">
        <
ListBox HorizontalAlignment="Left" Margin="79,72,0,149" Width="191"
IsSynchronizedWithCurrentItem="True" x:Name="myList">
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
            <
ListBoxItem>Item</ListBoxItem>
        </
ListBox>
        <
Button IsEnabled="{Binding Path=SelectedIndex,
Converter={StaticResource IntToBooleanRangeConverter},
ConverterParameter=1-, ElementName=myList}
"
HorizontalAlignment="Right" Margin="0,218,181,175"
Width="115" Content="Button" />
    </
Grid>
</
Window>

If the ListBox item zero is selected, the button is Disabled. ConverterParameter controls the results. It accepts either a single value for exact comparison, or a range, such as 0-10 or 5-. The latter would accept values 5 or higher and the former values zero through ten (inclusive).

Help support my web site by searching and buying through Amazon.com (in assocation with Amazon.com).