StringSplitterConverter–binding to a part of a string in XAML

December 21, 2014

no comments

I’m involved in a Windows Store project that needs to deal with localization. I already write a about localization in Windows Store application in January 2013 so you are welcome to read about it.

Our Xaml designers came to me the other day with an issue they found irritating. They needed to show some text on the screen that contain both regular text (some as bold, some italic etc.) and hyperlinks and if that’s not enough the text-resource was localized.

One way to solve this is to separate the text-resource to multiple parts and store each one independently but I was told that different languages might write the resource differently and they would prefer to have something they can do inside the XAML without needing to add work to the translators.

so I wrote a simple converter that enables to split a string by some  character and then specifying to the converter the part we wish it to return by using the converter parameter

public class StringSplitterConverter : IValueConverter
{
    public string Seperator { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var valueStr = value as string;
        int partIndex = int.Parse(parameter.ToString());
        string[] parts;
        string[] separators = new []{ Seperator};
        parts = valueStr.Split(separators,
            StringSplitOptions.RemoveEmptyEntries);
        if (parts.Count()>partIndex)
        {
            return parts[partIndex];
        }
        return String.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
 

and then in your XAML you use it like this:

<Page x:Class="MyPages.StringSplitterConverter"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:ooVoo.UI.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:converters="using:Converters"
      mc:Ignorable="d">
    <Page.Resources>
        <converters:StringSplitterConverter x:Key="StringSplitterConverter"
                                            Seperator="," />
    </Page.Resources>
    <Grid>                    
        <RichTextBlock TextAlignment="Center"                                   >
            <RichTextBlock.Blocks>
                <Paragraph TextAlignment="Center">
                    <Run FontWeight="SemiBold"
                         Text="{Binding ComplexLocalizedText, Source={StaticResource StringsResourceManager}, Converter={StaticResource StringSplitterConverter}, ConverterParameter=0}"/>
                    <Run Text="{Binding ComplexLocalizedText, Source={StaticResource StringsResourceManager}, Converter={StaticResource StringSplitterConverter}, ConverterParameter=1}"/>
                </Paragraph>
            </RichTextBlock.Blocks>
        </RichTextBlock>    
    </Grid>
</Page>
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*