This is my first blog in C# corner. In this
blog I want to share the code that I have written for converting numeric value
to its words format. It is very simple prototype and can be extended to be used
as 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 ex., 1 -> one, 2 -> two
For this purpose I have created Data Converter inherited from IValueConveter
interface provided by WPF library.
To create a value converter, we need to take four steps:
- Create a class that implements
IValueConverter.
- Add the ValueConversion attribute to the
class declaration, and specify the destination and target data types.
- Implement a Convert() method that changes
data from its original format to its display format.
- Implement a ConvertBack() method that
does the reverse and changes a value from display format to its native
format.
public
class
DataConverter : IValueConverter
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
word and return the string.
Coming to UI side, 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