Skip to content

Instantly share code, notes, and snippets.

@aroder
Created June 30, 2010 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aroder/459097 to your computer and use it in GitHub Desktop.
Save aroder/459097 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.Windows.Data;
/// <summary>
/// Converts a boolean value to a string value. For example, if the paramter passed in is "Elephant,Giraffe"
/// then a boolean true would convert to "Elephant" and false to "Giraffe"
/// </summary>
public class BooleanToStringCustomValueConverter : IValueConverter
{
#region Implemented Interfaces
#region IValueConverter
/// <summary>
/// The convert.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <param name="targetType">
/// The target type.
/// </param>
/// <param name="parameter">
/// Comma separated value. "TrueValue,FalseValue"
/// </param>
/// <param name="culture">
/// The culture.
/// </param>
/// <returns>
/// The convert.
/// </returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (null != parameter && ((string)parameter).Contains(","))
{
var parts = parameter.ToString().Split(System.Convert.ToChar(","));
return (bool)value ? parts[0] : parts[1];
}
return value.ToString();
}
/// <summary>
/// The convert back.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <param name="targetType">
/// The target type.
/// </param>
/// <param name="parameter">
/// The parameter.
/// </param>
/// <param name="culture">
/// The culture.
/// </param>
/// <returns>
/// The convert back.
/// </returns>
/// <exception cref="NotImplementedException">
/// </exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
#endregion
}
using System.Globalization;
using System.Windows.Data;
public class DecimalPlacesFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (null == value)
{
throw new ArgumentException("Parameter \"value\" cannot be null");
}
double number;
if (double.TryParse(value.ToString(), out number))
{
string format = "0";
try
{
if (null != parameter)
{
var decimalPlaces = int.Parse(parameter.ToString());
// if the number of decimal places requested is greater than 0, add the decimal
if (0 < decimalPlaces)
{
format += ".";
}
for (int i = 0; i < decimalPlaces && i < 10; i++)
{
format += "#";
}
}
} catch (Exception)
{
return string.Empty;
}
return number.ToString(format);
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
using System.Globalization;
using System.Windows.Data;
public class NumberFormatValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string format = "0";
if (null != parameter)
{
format = (string)parameter;
}
return (double.Parse(value.ToString())).ToString(format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return double.Parse(value.ToString());
}
}
using System.Collections.Generic;
using System.Linq;
public class TimeFormatValueConverter : System.Windows.Data.IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType == typeof(IEnumerable))
{
List<string> formattedDates = new List<string>();
foreach (var date in (IEnumerable<DateTime>) value)
{
formattedDates.Add(this.ConvertSingleDate(date, parameter));
}
return formattedDates.AsEnumerable();
}
DateTime parsedDate;
if (DateTime.TryParse(value.ToString(), out parsedDate))
{
return this.ConvertSingleDate(parsedDate, parameter);
}
return value;
}
private string ConvertSingleDate(DateTime date, object parameter)
{
// default to the "T" format, which is "hh:mm:ss AM/PM"
if (null == parameter)
{
return date.ToString("T");
}
return date.ToString((string)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType == typeof(DateTime))
{
return DateTime.Parse(value.ToString());
}
throw new NotImplementedException();
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment