WPF StringFormat and XAML Localization

OS: WP8

I am trying to format a string that is the result of a converter conversion. It all works except for the localization of the string format data, which I don't have the most vague idea of ​​how to include. Microsoft's documentation isn't all that clear and I'm wondering if anyone can point me in the right direction.

<TextBlock Text="{Binding Date, StringFormat='Received On: {0}', ConverterParameter=shortdatewithyear, Converter={StaticResource DateTimeToTimeConvert}}"/>

It doesn't sound like something to do on a wall.

Thank!

-Cord

+3


source to share


2 answers


In your specific case, I pulled the line from the resource file in the converter, then the provided .Net localization might work. This is probably more important when you are constructing strings and the order you create can change in different languages.

You create a resource file in the standard way - "MyResource.resx" to store strings for your default language, and then you can create a localized version called "MyResource.Fr-fr.resx" (if you were French). This will automatically load and search in the first instance for the string. If it doesn't find one, the code will pull the line from the default resource file. This way, you don't have to translate everything that is useful for differences in US / GB spelling.

In general, if you have this, you can have localized strings in your XAML

Add Localize class:

public class Localize : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyChange(String name)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    #endregion

    #region 'Public Properties'

    //Declarations
    private static Resources.MyResources _myResources = new Resources.MyResources();

    public Resources.MyResources myResources
    {
        get { return _myResources; }
        set { NotifyChange("MyResources"); }
    }

    #endregion
}

      



Then in your XAML add this to your user management resources:

<local:Localize x:Key="myResource"
                xmlns:local="clr-namespace:MyProject" />

      

Then you can use it:

<TextBlock Text="{Binding myResource.MyString, Source={StaticResource myResource}}"/>

      

+2


source


One way to handle this without using additional converters or modifying the base model is to split the string into two separate interface elements. For example, two TextBlock

inside a StackPanel

, for example:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{x:Static properties:Resources.ReceivedOn}" Margin="0,0,5,0"/>
    <TextBlock Text="{Binding Date, ConverterParameter=shortdatewithyear, Converter={StaticResource DateTimeToTimeConvert}}"/>
</StackPanel>

      



Thus, you can use the usual localization for the "Received On:"

0


source







All Articles