Question

I'm not that firm with MVVM and I hope someone can help me with this. I'm using C#/XAML for Windows Phone 8. Usually my ViewModel provides a property MyProperty and I'd bind it like this:

<TextBlock Text="{Binding MyProperty, StringFormat='This Property: {0}'}" FontSize="30" />

The problem is that in my view model there are some data bound properties which are localized by different strings. E.g. let's say you have a date - either upcoming or aleady passed. This date shall be localized like this:

upcoming: "The selected date {0:d} is in the future"
passed: "The selected date {0:d} already passed"

Is it possible to do the localization in the XAML? Or is there another possiblity to avoid localized strings in the viewmodel? (Is an avoidance of localized strings in the viewmodel desirable after all?)

Thanks in advance!

Regards, Marc

Was it helpful?

Solution

Try using a IValueConverter

Example:

Xaml:

<Window x:Class="ConverterSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ConverterSpike="clr-namespace:ConverterSpike"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ConverterSpike:DateStringConverter x:Key="DateConverter" />
    </Window.Resources>
    <Grid>
        <TextBlock  Text="{Binding MyProperty, Converter={StaticResource DateConverter}}" />
    </Grid>
</Window>

Converter:

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

namespace ConverterSpike
{
    public class DateStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return string.Empty;
            var date = value as string;
            if (string.IsNullOrWhiteSpace(date)) return string.Empty;

            var formattedString = string.Empty; //Convert to DateTime, Check past or furture date, apply string format

            return formattedString;
        }

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

OTHER TIPS

If you're looking to specify a fixed format within the Xaml, it does support format strings for things like dates. Use StringFormat={0:'dd/MM/yyyy'} for instance for DMY. It also supports a number of pre-defined formats as well. Just do a search for "date format in xaml bindings" for details.

You can create localized resorce files:

String.xaml

<system:String x:Key="MyKey">English {0} {1}</system:String>

String.de-DE.xaml

<system:String x:Key="MyKey">{0} Deutsch {1}</system:String>

And than use strings in your views by key. If you need placeholders, you can fill them with multibinding like this:

<TextBlock>
   <TextBlock.Text>
      <MultiBinding StringFormat="{StaticResource MyKey}">
            <Binding Path="MyObject.Property1" />
            <Binding Path="MyObject.Property2" />
      </MultiBinding>
   </TextBlock.Text>
</TextBlock>

If MyObject.Property1 is "text1" and MyObject.Property2 is "text2", result for English will be "English text1 text2" and for German "text1 Deutsch text2"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top