|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MultiplyConverter.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Calculate the product of given value and factor in parameter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows.Data;
namespace Catel.Windows.Data.Converters
{
/// <summary>
/// Calculate the product of given value and factor in parameter.
/// </summary>
#if !SILVERLIGHT
[ValueConversion(typeof(int), typeof(int))]
#endif
public class MultiplyConverter : IValueConverter
{
/// <summary>
/// Calulate the product of given value and factor in parameter.
/// </summary>
/// <param name="value">Base value.</param>
/// <param name="targetType">Not used, supports now int and double.</param>
/// <param name="parameter">Factor used for multiply (always double).</param>
/// <param name="culture">Not used.</param>
/// <returns>Product of specified values.</returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Get typed value
double typedValue = 0d;
// Check input
if (value is int)
{
typedValue = System.Convert.ToDouble((int)value);
}
else if (value is double)
{
typedValue = (double)value;
}
// Check value
if (typedValue == 0d)
{
return 0d;
}
// Get factor
double factor;
if (!double.TryParse(parameter as string, out factor))
{
return 0d;
}
// Multiply
return typedValue * factor;
}
/// <summary>
/// Calculate value back.
/// </summary>
/// <param name="value">Base value.</param>
/// <param name="targetType">Not used, supports now int and double.</param>
/// <param name="parameter">Factor used for multiply (always double).</param>
/// <param name="culture">Not used.</param>
/// <returns>Original value.</returns>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Get typed value
double typedValue = 0d;
// Check input
if (value is int)
{
typedValue = System.Convert.ToDouble((int)value);
}
else if (value is double)
{
typedValue = (double)value;
}
// Check value
if (typedValue == 0d)
{
return 0d;
}
// Get factor
double factor;
if (!double.TryParse(parameter as string, out factor))
{
return 0d;
}
// Divide
return typedValue / factor;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.