Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF Simple Data Converter Example

0.00/5 (No votes)
3 Oct 2013 2  
A simple data converter example in WPF

Introduction

In this tip, I would like to share the code that I have written for converting numeric value to its words format. It is a very simple prototype and can be extended to be used as a full fledged application or user control.

In this example, I have a list of numbers which is to be populated in combo box. At the time of displaying numbers in combo box, they should be shown in words. For example, 1 -> one, 2 -> two …..

For this purpose, I have created a Data Converter inherited from IValueConveter interface provided by the WPF library.

public class DataConverter : IValueConverter 

Whenever any class is implementing IValueConverter, it needs to implement two methods provided by interface:

public object Convert(object value, Type targetType, 
object parameter, System.Globalization.CultureInfo culture) 
public object ConvertBack(object value, Type targetType, 
object parameter, System.Globalization.CultureInfo culture) 

In Convert method, I have written the logic to convert numeric value into words and return the string.

Coming to the UI side, the window contains just a combo box to be populated with list using data context (binding). The list contains all the numbers, hence we need to put converter to convert numeric values to words.

<ComboBox
 ItemsSource="{Binding list,  
 Converter={StaticResource DataConverter}}">

Finally the application looks like:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here