Posted by: Zeeshan Amjad | August 1, 2011

Range Converter Revisited


We already saw one example of Range Converter here. But in this example we created two different classes, one to implement the IValueConverter interface and other child class of MarkupExtension to make it markup extension. We can do both work in the same class. We can inherit class from MarkupExtension class and at the same time implement the IValueConverter interface.

Here is simple implementation of our class.

Code Snippet
public class RangeExtension : MarkupExtension, IValueConverter
{
    public int Maximum
    { get; set; }

    public int Minimum
    { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int data = (int)value;

        if (data >= Minimum && data <= Maximum)
            return true;
        else
            return false;

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

Now we can use this class easily in XAML. Here is one example to use this example in Data Trigger and set the foreground and background color of all cities whose population is in between 100,000 and 200,000.

Code Snippet
<Style TargetType="{x:Type ListViewItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Populatoin,
            Converter={local:Range Maximum=2000000, Minimum=1000000}}" Value="true">
            <Setter Property="Background" Value="Green"/>
            <Setter Property="Foreground" Value="White"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

 

Here is complete C# code of the program.

Code Snippet
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfRangeExtension
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<CityInfo> cities = new ObservableCollection<CityInfo>();

        public MainWindow()
        {
            InitializeComponent();

            cities.Add(new CityInfo() { State = "California", City = "Los Angeles", Populatoin = 3831868 });
            cities.Add(new CityInfo() { State = "California", City = "San Francisco", Populatoin = 3228605 });
            cities.Add(new CityInfo() { State = "California", City = "Sacramento", Populatoin = 486189 });
            cities.Add(new CityInfo() { State = "California", City = "San Diego", Populatoin = 1359132 });
            cities.Add(new CityInfo() { State = "Massachusetts", City = "Boston", Populatoin = 645169 });
            cities.Add(new CityInfo() { State = "Massachusetts", City = "Newton", Populatoin = 93447 });

            DataContext = cities;
        }
    }

    public class RangeExtension : MarkupExtension, IValueConverter
    {
        public int Maximum
        { get; set; }

        public int Minimum
        { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int data = (int)value;

            if (data >= Minimum && data <= Maximum)
                return true;
            else
                return false;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class CityInfo
    {
        public String State
        { set; get; }

        public String City
        { set; get; }

        public int Populatoin
        { set; get; }
    }
}

 

And here is complete XAML code of the program.

Code Snippet
<Window x:Class="WpfRangeExtension.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:local="clr-namespace:WpfRangeExtension"       
        Title="Range Markup Extension" Height="300" Width="400">
    <Window.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Populatoin,
                    Converter={local:Range Maximum=2000000, Minimum=1000000}}" Value="true">
                    <Setter Property="Background" Value="Green"/>
                    <Setter Property="Foreground" Value="White"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{Binding}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="120" Header="State" DisplayMemberBinding="{Binding Path = State}"/>
                    <GridViewColumn Width="120" Header="City" DisplayMemberBinding="{Binding Path = City}"/>
                    <GridViewColumn Width="120" Header="Populatoin" DisplayMemberBinding="{Binding Path = Populatoin}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

 

This is the output of the program.

RangeMarkuExtension


Responses

  1. […] markup extension. We are going to use the same technique to use value converter in markup extension here. We are gong to do the same, i.e. create a class that is child class of MarkupExtension class and […]

  2. […] interface. We already saw few examples of markup extension which implement IValueConverter here and […]


Leave a comment

Categories