1

I have several string resources for different languages. As you can see, all of them starts with capitalized letter and then in lower case. So, is there any way how to Converter all of them to UPPERCASE without direct changing of resources? Is it possible to do it in XAML? Maybe combination of Binding's converter?

enter image description here

2 Answers2

1

Here's a C# version of the same thing:

public class ToUpperConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string stringValue = value as string;
        return string.IsNullOrEmpty(stringValue) ? string.Empty : stringValue.ToUpper();
    }

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

To reference this in XAML:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1">

    <Page.Resources>
        <local:ToUpperConverter x:Key="UpperCaseConverter" />
    </Page.Resources>

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="{x:Bind MyString, Converter={StaticResource UpperCaseConverter}}" />
    </StackPanel>
</Page>

For completeness, this is the property I x:Bind to:

public sealed partial class MainPage
{
    public string MyString => "Hello world!";

    public MainPage()
    {
        InitializeComponent();
    }
}

EDIT

In the comments of the OP, @RaamakrishnanA asks about how this might work with resources. A bit of indirection is one approach.

In the .resw file, provide a value for the Tag property:

<data name="HelloWorld.Tag">
  <value>Hello, world!</value>
</data>

Now use x:Uid to bind that to the Tag property of a TextBlock, then bind the Text property to the tag, allowing us to use the converter:

<TextBlock
    x:Name="textBlock"
    x:Uid="HelloWorld"
    Text="{Binding Tag, ElementName=textBlock, Converter={StaticResource UpperCaseConverter}}"
/>

Output:

enter image description here

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
0

You told the answer yourself. Use a converter.

Public Namespace Converter
    Public Class ToUpperValueConverter
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Dim str = TryCast(value, String)
            Return If(String.IsNullOrEmpty(str), String.Empty, str.ToUpper())
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Return Nothing
        End Function
End Class

Edit

To use this converter you need to use some kind of binding to your property and not the usual x:Uid way. You can't bind to a resource directly. Instead, you need to convert the resource into some form of code behind and bind it through your ViewModel. This SO answer will walk you through the steps. But instead of the PublicResXFileCodeGenerator tool, you may have to use something like ResW File Code Generator

I am afraid there is no way to do this in pure XAML.

Community
  • 1
  • 1
Raamakrishnan A.
  • 515
  • 3
  • 14
  • Dear @Raamakrishnan A., I know how to write the converter for binding statement, but could you please provide some implementation of that in XAML? For example I have so how can I format the resource that is taken from the .resx file? – Konstantin Chsherbakov Jun 02 '16 at 18:30
  • @KonstantinChsherbakov I have changed my answer accordingly. I hope it helps you – Raamakrishnan A. Jun 03 '16 at 07:25