Quantcast
Channel: Panorama – Metro Nuggets
Viewing all articles
Browse latest Browse all 4

Application Name Display Different Cases

0
0

The application’s name can appear in many different places in your app, and in those different places there can be different ways in which that should be displayed and what case should be used. For example, if you have the app name as the title in a panoramic page, it should be in lower case, whereas in a pivot page (or a normal page), you would usually show the app name all in capitals. So what’s the best way of doing this? Have three different types of application name (Upper, Lower, Normal)? Not ideal really.

The Solution

I’ve created a converter that will solve this problem and mean that you only need to store the application name once and in one style (Normal).

using System;
using System.Windows.Data;

namespace ScottIsAFool.WindowsPhone.Converters
{
    public class StringCaseConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string textToConvert = value.ToString();
            bool isToLower = true;
            if (parameter != null) isToLower = System.Convert.ToBoolean(parameter.ToString());

            return isToLower ? textToConvert.ToLower() : textToConvert.ToUpper();
        }

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

Just add this converter to your project and you’re set.

Usage

In your xaml you will have to have a binding set up (whether part of your viewmodel or just pointing to a StaticResource). By default if you put no ConverterParameter in it will make it lower case.

Title="{Binding AppName, Converter={StaticResource StringCaseConverter}}"

So if you want it in upper case then you would do

Text="{Binding AppName,
               Converter={StaticResource StringCaseConverter},
               ConverterParameter=false}"

If you are storing the application name as a StaticResource, then it’s simply

Text="{Binding Source={StaticResource AppName},
               Converter={StaticResource StringCaseConverter},
               ConverterParameter=false}"

There you have it. If you want the text to appear exactly how you typed it, then just omit the Converter altogether. Obviously I’m using the application name as an example, but this converter can be used against any string.

SL



Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images