Integration

In your XAML code, you need to use the Enough.Converter namespace, add a corresponding resource and then use the converter.

The XML namespace (xmlns) has to be used differently on Windows RT and on Windows Phone Silverlight :

Windows 8, Windows Phone RT and Universal Projects

<UserControl
    xmlns:xamlconverter="using:Enough.Converter"
    ...
>
    <UserControl.Resources>
        <xamlconverter:WeekdayNameConverter x:Name="WeekdayNameConverter" />
        <xamlconverter:DateAbbreviationConverter x:Name="DateAbbreviationConverter" />
    </UserControl.Resources>
    <StackPanel>
            <TextBlock Text="{Binding Date, Converter={StaticResource WeekdayNameConverter}}" />
            <TextBlock Text="{Binding Date, Converter={StaticResource DateAbbreviationConverter}}" />
    </StackPanel>
</UserControl>

Windows Phone Silverlight

<UserControl
    xmlns:xamlconverter="clr-namespace:Enough.Converter;assembly=Enough.WP.XamlConverter"
    ...
>
    <UserControl.Resources>
        <xamlconverter:WeekdayNameConverter x:Name="WeekdayNameConverter" />
        <xamlconverter:DateAbbreviationConverter x:Name="DateAbbreviationConverter" />
    </UserControl.Resources>
    <StackPanel>
            <TextBlock Text="{Binding Date, Converter={StaticResource WeekdayNameConverter}}" />
            <TextBlock Text="{Binding Date, Converter={StaticResource DateAbbreviationConverter}}" />
    </StackPanel>
</UserControl>

NuGet

Enough.XamlConverter is available as a NuGet package. Right-click your project, select "Manage NuGet Packages..." and search for "Enough.XamlConverter". Or open your Package Manager Console and type:

Install-Package Enough.XamlConverter

Available Converters

Note that we assume that you use the same resource key as the name of the converter in the below examples.

BackgroundColorToBWForegroundConverter

Converts a color to it's grayscale value and then decides if a black or a white foreground color is better suited.
<StackPanel Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}">
    <TextBlock Text="Hello World" Foreground="{Binding Color, Converter={StaticResource BackgroundColorToBWForegroundConverter}}" />
</StackPanel>
This converter cannot convert back.

BooleanNegationConverter

Converts a boolean value to it's opposite.
<CheckBox IsChecked="{Binding DisableFunctionality, Converter={StaticResource BooleanToNegationConverter}, Mode=TwoWay}" />
The BooleanNegationConverter can also convert backwards.

BooleanToVisibilityConverter

Converts a boolean value to it's corresponding visibility value: true results in Visible and false in Collapsed.
<TextBlock Text="Hello World" Visibility="{Binding ShowItToTheWorld, Converter={StaticResource BooleanToVisibilityConverter}}" />

You can also specify the 'reverse' parameter to reverse the output (then false results into Visible and true results in Collapsed):
<TextBlock Text="Hello World" Visibility="{Binding ShowItToTheWorld, Converter={StaticResource BooleanToVisibilityConverter},ConverterParameter='reverse'}" />
The BooleanToVisibilityConverter can also convert backwards.

CollectionSizeToBooleanConverter

Works as the CollectionSizeToVisibilityConverter but return bool values instead of Visible/Collapsed. See below for more information.

CollectionSizeToVisibilityConverter

The CollectionSizeToVisibilityConverter makes an element visible when the corresponding collection is not empty or has the specified minimum number of objects.
<TextBlock Text="You have not created any bookmarks yet." Visibility="{Binding BookmarkList, Converter={StaticResource CollectionSizeToVisibilityConverter },ConverterParameter='0'}"/>
Use 'reverse' to reverse the visibility output and a number to specify the required size of the collection in the ConverterParameter.
The converter returns 'Visibility.Collapsed' when the collection is null.

Note that you need to update the databound value in the view model when changing the collection, otherwise the change is not picked up:
private ObservableCollection<Bookmark> _bookmarkList= new ObservableCollection<Bookmark>();
public ObservableCollection<Bookmark> BookmarkList
{
   get { return _bookmarkList; }
   set
   {
        _bookmarkList= value;
        RaisePropertyChanged();
   }
}

[...]
public void ClearBookmarks()
{
   _bookmarkList.Clear();
  BookmarkList = _bookmarkList; // alternatively: RaisePropertyChanged("BookmarkList")
}

public void AddBookmark(Bookmark bookmark)
{
    _bookmarkList.Add(bookmark);
    BookmarkList = _bookmarkList;  // alternatively: RaisePropertyChanged("BookmarkList")
}
This converter cannot convert backwards.

ColorToBrushConverter

The ColorToBrushConverter converts a color to a solid color brush:
<StackPanel Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}">
    <TextBlock Text="Hello World" />
</StackPanel>
This converter cannot convert back.

DateAbbreviationConverter

The DateAbbreviationConverter converts a DateTime into it's short textual representation which is locale dependent.
<TextBlock Text="{Binding Date, Converter={StaticResource DateAbbreviationConverter}}" />

DateFullConverter

The DateFullConverter converts a DateTime into it's long textual representation which his locale dependent.
<TextBlock Text="{Binding Date, Converter={StaticResource DateFullConverter}}" />

DateTimeConverter

The DateTimeConverter converts a DateTime into it's short textual representation and the current time (both localized).
<TextBlock Text="{Binding SubmissionDate, Converter={StaticResource DateTimeConverter}}" />

HexStringToColorConverter

The HexStringToColorConverter converts a web hexadecimal color like ff00ff into a color.
<perpetuum:ColorPicker Color="{Binding FontColor, Mode=TwoWay, Converter={StaticResource HexStringToColorConverter}}" />
The HexStringToColorConverter can convert backwards.

HexStringToSolidColorBrushConverter

The HexStringToSolidColorBrushConverterconverts a web hexadecimal color like ff00ff into a solid color brush.
<StackPanel Background="{Binding ColorString, Converter={StaticResource HexStringToSolidColorBrushConverter}}" />
The HexStringToSolidColorBrushConvertercan convert backwards.

MaxStringLengthToBooleanConverter

The MaxStringLengthToBooleanConverter converters checks if a string has at most the specified number of characters before returning true.
<AppBarButton Icon="ReShare" Label="Share message"  IsEnabled="{Binding Message,Converter={StaticResource MaxStringLengthToBooleanConverter}, ConverterParameter='140'}"/>

MaxStringLengthToVisibilityConverter

The MaxStringLengthToVisibilityConverter converter checks if a string has at most the specified number of characters before returning Visible.
<AppBarButton Icon="ReShare" Label="Share message"  Visibility="{Binding Message,Converter={StaticResource MaxStringLengthToVisibilityConverter}, ConverterParameter='140'}"/>

MinStringLengthToBooleanConverter

The MinStringLengthToBoolean converters checks if a string has at least the specified number of characters before returning true.
<AppBarButton Icon="ReShare" Label="Share message"  IsEnabled="{Binding Message,Converter={StaticResource MinStringLengthToBoolean }, ConverterParameter='10'}"/>

MinStringLengthToVisibilityConverter

The MinStringLengthToVisibilityConverterconverters checks if a string has at least the specified number of characters before returning Visible.
<AppBarButton Icon="ReShare" Label="Share message"  Visibility="{Binding Message,Converter={StaticResource MinStringLengthToVisibilityConverter}, ConverterParameter='10'}"/>

NumberToTextConverter

Converts the given double or integer number to a local-sensitive textual representation with grouping and - for double values - with the local specific number of fraction digits. You can optionally specify the number of the fraction digits by providing a ConverterParameter:
<TextBlock Text="{Binding NumberOfDownloadsPerDay, Converter={StaticResource NumberToTextConverter},ConverterParameter=3}" />

StringFormatConverter

Use any available String.Format format pattern to format your databound value:
<TextBlock Text="{Binding NumberOfDownloadsPerDay, Converter={StaticResource StringFormatConverter},ConverterParameter='\{0:N2\}'}" />
<TextBlock Text="{Binding ElapsedTime, Converter={StaticResource StringFormatConverter},ConverterParameter='\{0:mm\\:ss\}'}" />
For a list of available format patterns please refer to the MSDN documentation: http://msdn.microsoft.com/library/system.string.format.aspx.

TimeSpanConverter

The TimeSpanConverter translates a TimeSpan into a human-readable form. For example a TimeSpan.FromDays(6) will result in "6 days" while TimeSpan.FromDays(1) results in "1 day".

You can control the translations by providing localized resources in your Resources.resw or Resources.resx files. The final output will depend on the timeframe and the available translations:

For TimeSpans longer than 365 days:

We will calculate the years and months roughly and use the most specific available translations:
TimeSpan_Format_YearsAndMonths: when there are several years and months
TimeSpan_Format_YearsAndMonth: when there are several years and a single month
TimeSpan_Format_YearAndMonths: when there is one year and several months
TimeSpan_Format_YearAndMonth: when there is one year and one month

When the above are not defined, we will look for the following translations:
TimeSpan_Format_Years: when there are several years
TimeSpan_Format_Year: when there is a single year

When the above are not defined, we will use "{0} years" or "1 year" as the default.

For TimeSpans longer than 30 days:

We will calculate the number of months and days and use one of the following translations, if it is available:
TimeSpan_Format_MonthsAndDays: for several months and several days
TimeSpan_Format_MonthsAndDay: for several month and a single day
TimeSpan_Format_Months: for several months and no day
TimeSpan_Format_MonthAndDays: for a single month and several days
TimeSpan_Format_MonthAndDay: for a single month and a single day
TimeSpan_Format_Month: for a single month

If none of the above is defined, we will use the number of weeks and days:
TimeSpan_Format_WeeksAndDays: for several weeks and several days
TimeSpan_Format_WeeksAndDay: for several weeks and one day
TimeSpan_Format_Weeks: for several weeks and no day
TimeSpan_Format_WeekAndDays: for one week and several days
TimeSpan_Format_WeekAndDay: for one week and one day
TimeSpan_Format_Week: for one week

If none of the above is defined, we will fall back to the days translations described below.

For TimeSpans longer than a day:

We use the number of days and use one of the following fitting translation:
TimeSpan_Format_Days: for several days
TimeSpan_Format_Day: for a single day

We will fall back to "{0} days" and "1 day" if none of the above translations is defined.

For TimeSpans longer than an hour:

We use the number of hours and one of the following translations:
TimeSpan_Format_Hours: several hours
TimeSpan_Format_Hour: for one hour

We will fall back to "{0} hours" or "1 hour" if none of the above translations is defined.

For TimeSpans longer than a minute:

We use the number of minutes and one of the following translations:
TimeSpan_Format_Minutes: several minutes
TimeSpan_Format_Minute: for one minute

We will fall back to "{0} minutes" or "1 minute" if none of the above translations is defined.

For all other TimeSpans:

We use the number of seconds and one of the following translations:
TimeSpan_Format_Seconds: several or 0 seconds
TimeSpan_Format_Second: for one second

We will fall back to "{0} seconds" or "1 second" if none of the above translations is defined.

Example Translations

  <data name="TimeSpan_Format_Day" xml:space="preserve">
    <value>1 day</value>
  </data>
  <data name="TimeSpan_Format_Days" xml:space="preserve">
    <value>{0} days</value>
  </data>
  <data name="TimeSpan_Format_Hour" xml:space="preserve">
    <value>1 hour</value>
  </data>
  <data name="TimeSpan_Format_Hours" xml:space="preserve">
    <value>{0} hours</value>
  </data>
  <data name="TimeSpan_Format_Minute" xml:space="preserve">
    <value>1 minute</value>
  </data>
  <data name="TimeSpan_Format_Minutes" xml:space="preserve">
    <value>{0} minutes</value>
  </data>
  <data name="TimeSpan_Format_Month" xml:space="preserve">
    <value>1 month</value>
  </data>
  <data name="TimeSpan_Format_MonthAndDay" xml:space="preserve">
    <value>1 month</value>
  </data>
  <data name="TimeSpan_Format_MonthAndDays" xml:space="preserve">
    <value>1 month and {0} days</value>
  </data>
  <data name="TimeSpan_Format_Months" xml:space="preserve">
    <value>{0} months</value>
  </data>
  <data name="TimeSpan_Format_MonthsAndDay" xml:space="preserve">
    <value>{0} months</value>
  </data>
  <data name="TimeSpan_Format_MonthsAndDays" xml:space="preserve">
    <value>{0} months and {0} days</value>
  </data>
  <data name="TimeSpan_Format_Second" xml:space="preserve">
    <value>1 second</value>
  </data>
  <data name="TimeSpan_Format_Seconds" xml:space="preserve">
    <value>{0} seconds</value>
  </data>
  <data name="TimeSpan_Format_Year" xml:space="preserve">
    <value>1 year</value>
  </data>
  <data name="TimeSpan_Format_YearAndMonth" xml:space="preserve">
    <value>1 year and 1 month</value>
  </data>
  <data name="TimeSpan_Format_YearAndMonths" xml:space="preserve">
    <value>1 year and {0} months</value>
  </data>
  <data name="TimeSpan_Format_YearsAndMonth" xml:space="preserve">
    <value>{0} years</value>
  </data>
  <data name="TimeSpan_Format_YearsAndMonths" xml:space="preserve">
    <value>{0} years</value>
  </data>

TranslationConverter

The TranslationConverter translates the given ConverterParameter and exchanges {0} with the data bound value.

This example displays "10 albums" when the resource "AlbumHeader_AvailableAlbums" is set to "{0} albums" and when the data bound "Count" property is 10:

<TextBlock FontFamily="10" Text="{Binding Count, Converter={StaticResource TranslationConverter}, ConverterParameter='AlbumHeader_AvailableAlbums'}" HorizontalAlignment="Right" VerticalAlignment="Bottom" />

The above example displays "one album" when the resource "AlbumHeader_AvailableAlbums1" is set to "one album" and when the data bound "Count" property is 1. So the TranslationConverter will first look for the "ConverterParameter + bound value" translation before falling back to just the ConverterParameter translation.

UppercaseConverter

The UppercaseConverter converts text into uppercase characters.
<TextBlock Text="{Binding Title, Converter={StaticResource UppercaseConverter}}"/>

ValueNotNullToBooleanConverter

The ValueNotNullToBooleanConverter reverts a value that is not null into true. When you specify the 'reverse' ConverterParameter null will result into true.
<Button Command="{Binding PlayAudioCommand}" IsEnabled="{Binding Chapter.Audio, Converter={StaticResource ValueNotNullToBooleanConverter}}" />
This converter cannot convert backwards.

ValueNotNullToVisibilityConverter

The ValueNotNullToVisibilityConverterreverts a value that is not null into visible. When you specify the 'reverse' ConverterParameter null will result into visible.
<TextBox Text="Play Audio" Visibility="{Binding Chapter.Audio, Converter={StaticResource ValueNotNullToVisibilityConverter}}" />

WeekdayNameConverter

The WeekdayNameConverter converts a DateTime into the locale dependent name of the corresponding weekday. Additionally Yesterday, Today and Tomorrow are handled as special cases. You need to provide translations for these values in your Resources.resw or Resources.resx files.
<TextBlock Text="{Binding Date, Converter={StaticResource WeekdayNameConverter}}" />

Last edited Mar 31, 2015 at 11:22 AM by enoughrob, version 11