WPF Value Conversion with IValueConverter


Data binding is a concept that is a little more complex than many other WPF concepts, including controls and their properties. Over at WPF Tutorial has some articles on data binding. For this post, I will do something a bit different and more simple. There is a follow-up to this post that looks at multiple converters in the same app.

In this program, the user enters some text in a text box and I use data binding to bind it to the TextBlock.

Here is the XAML code.

<Window x:Class="ConvertAandB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ConvertAandB"
        mc:Ignorable="d"
        Title="ConvertAandB" Height="150" Width="400">
    <Window.Resources>
        <local:UpperABToLower x:Key="UpperABToLower" />
    </Window.Resources>
    <StackPanel Margin="16">
        <TextBox Name="txtboxIn" Width="200" HorizontalAlignment="Left">Enter text</TextBox>
        <WrapPanel >
            <TextBlock Text="Convert A to a and B to b:   " />
            <TextBlock Text="{Binding ElementName=txtboxIn, Path=Text, 
                Converter={StaticResource UpperABToLower}}"></TextBlock>
        </WrapPanel>
    </StackPanel>
</Window>

Here is the class file I created. It is called UpperABToLower.cs.

using System;
using System.Globalization;
using System.Windows.Data;

namespace ConvertAandB
{
    class UpperABToLower : IValueConverter
    {
        // Convert all 'A' to 'a' and all 'B' to 'b'
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stg = value.ToString().Replace('A', 'a');
            stg = stg.ToString().Replace('B', 'b');
            return stg;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Here is the code-behind for the main window. I have not altered this code at all.

using System.Windows;
namespace ConvertAandB
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

Here is what the Solution Explorer looks like.

Multiple Conversion Tasks

If you need to do multiple things, you can always call other methods from your converter. How? Below is an example of how you would also convert C.

using System;
using System.Globalization;
using System.Windows.Data;

namespace ConvertAandB
{
    class UpperABToLower : IValueConverter
    {
        // Convert all 'A' to 'a' and all 'B' to 'b' and 'C' to 'c'
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stg = value.ToString().Replace('A', 'a');
            stg = stg.ToString().Replace('B', 'b');
            
            // Convert 'C' to 'c'
            stg = ConvertCToc(stg);  // call another 'conversion'
            return stg;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public string ConvertCToc(string inText)
        {
            return inText.ToString().Replace('C', 'c');
        }
    }
}