WPF Value Conversion with Multiple IValueConverters


This follows from the last post. In a program, you may have the need for multiple converters.

In the C# code below (UpperABTolower.cs) I have added a new class at the bottom called ConvertUpperVowelsToLower. The code here is not at all complex. You are more likely to make a mistake in the XAML code.

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');
        }
    }
    class ConvertUpperVowelsToLower : IValueConverter
    {
        // Convert all 'A' to 'a', 'E' to 'e', 'I' to 'i', 'O' to 'o', 'U' to 'u'
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stg = value.ToString().Replace('A', 'a');
            stg = stg.ToString().Replace('E', 'e');
            stg = stg.ToString().Replace('I', 'i');
            stg = stg.ToString().Replace('O', 'o');
            stg = stg.ToString().Replace('U', 'u');
            return stg;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Below is the XAML. Notice that there are now two Window.Resources.

<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="350" Width="400">
    <Window.Resources>
        <local:UpperABToLower x:Key="UpperABToLower" />
        <local:ConvertUpperVowelsToLower x:Key="ConvertUpperVowelsToLower" />
    </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 and C to c:   " />
            <TextBlock Text="{Binding ElementName=txtboxIn, Path=Text, 
                Converter={StaticResource UpperABToLower}}"></TextBlock>
        </WrapPanel>
        <TextBox Name="txtboxIn2" Width="200" HorizontalAlignment="Left">Enter text</TextBox>
        <WrapPanel>
            <TextBlock Text="Convert upper case vowels to lower case vowels:   " />
            <TextBlock Text="{Binding ElementName=txtboxIn2, Path=Text, 
                Converter={StaticResource ConvertUpperVowelsToLower}}"></TextBlock>
        </WrapPanel>
    </StackPanel>
</Window>