Recently I was asked by a colleague how I would implement a WPF Combobox which has a binding to some classes implemented as a strategy pattern. I think this is a pretty common situation and was surprised that I could not find anything on Google so I decided to write it down by myself.

window

Introduction

Code samples

To make this article readable I decided to show only code snippets during the article, but keep in mind that you can access the entire code on Bitbucket or download the entire solution (18kb). I recommend using one of these resources while reading through the following sections.

We have a minimalistic vehicle lending software with only a single window which calculates the price for the lend based upon the input parameters.

Classes involved

  • MainWindow and MainWindowViewModel are the classes involved in the classic MVVM pattern
  • IVehicleCategory and its realizations Airplane and Car are the involved strategy pattern implementation. Each Vehicle implements its own algorithm to calculate the price for the lend.

Class diagram

 Combobox definition

The MainWindowViewModel.xaml file contains the definition of the Combobox we are interested in. The following snippet shows how to declare a Combobox using the properties ItemsSource and SelectedItem.

<ComboBox ItemsSource="{Binding Categories}" 
		  SelectedItem="{Binding Category}" />

MainWindowViewModel

The ViewModel class contains two properties which are defined in the XAML snippet above. First there is a list called Categories containing all elements which can be selected in the Combobox. Next there is a property called Category which holds the reference to the currently selected strategy.

public IList<IVehicleCategory> Categories { get; private set; }

private IVehicleCategory _category;
public IVehicleCategory Category
{
	get { return _category; }
	set
	{
		_category = value;
		OnPropertyChanged("CalculatedPrice");
	}
}

The most important part here is, that the Category property calls the OnPropertyChanged method to indicate that the binding to the calculated price should be refreshed whenever the strategy is changed.

The implementation of the CalculatedPrice property is very simple as you can see here:

public string CalculatedPrice
{
	get
	{
		return Category.GetPrice(To.AddDays(1) - From) + " $";
	}
}

The only thing we need to do is to access the category implementation via the Category property and call the GetPrice method on it.

Price strategies

To give you an understanding of an implementation of the IVehicleCategory interface, I show you the implementation of the Car class.

public class Car : IVehicleCategory
{
	private decimal _pricePerDay;

	public Car()
	{
		_pricePerDay = 12.5m;
	}

	public decimal GetPrice( TimeSpan duration )
	{
		return duration.Days * _pricePerDay;
	}

	public override string ToString()
	{
		return "Car";
	}
}

Extensibility

If we want to extend the functionality of our system and implement a skateboard for example, we just have to add a class called Skateboard and implement the IVehicleCategory interface and add a reference to the new object to the Categories list.

Conclusion

This a pretty easy way to bind some implementations of different behaviors to a WPF Combobox. Please let me know if you have some improvements or whether you like this way of doing so or not.

Resources

Share this

Claudio Bernasconi

Claudio Bernasconi is a professional software developer, Microsoft certified technology specialist, blogger, technology enthusiast and founder of CarParking Schweiz. Since 2010 he works for KMS AG in Lucerne, Switzerland.