// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReferenceToBooleanConverter.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Implementation of class ReferenceToBooleanConverter
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Windows.Data;
namespace Catel.Windows.Data.Converters
{
/// <summary>
/// Implementation of class ReferenceToBooleanConverter
/// </summary>
#if !SILVERLIGHT
[ValueConversion(typeof(object), typeof(bool))]
#endif
public class ReferenceToBooleanConverter : IValueConverter
{
/// <summary>
/// Convert an object reference to a boolean value.
/// </summary>
/// <param name="value">The object reference to evaluate.</param>
/// <param name="targetType">A targettype, currently not used.</param>
/// <param name="parameter">A additional parameter</param>
/// <param name="culture">A culture, currently not used.</param>
/// <returns>
/// When <paramref name="value"/> is not null the method returns true else false.
/// Unless the passed <paramref name="parameter"/> parameter is true, then the
/// output is inverted; meaning when <paramref name="value"/> is null the method
/// returns true else false.
/// </returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool isNull = value == null;
bool inverseValue = false;
if (parameter != null)
{
inverseValue = TypeHelper.Cast<bool, object>(parameter);
}
return inverseValue ? isNull : !isNull;
}
/// <summary>
/// Convert nothing!
/// </summary>
/// <param name="value">A value.</param>
/// <param name="targetType">A targettype, currently not used.</param>
/// <param name="parameter">A parameter value, currently not used.</param>
/// <param name="culture">A culture, currently not used.</param>
/// <returns>Nothing, only throws an exception.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ConverterHelper.DoNothingBindingValue;
}
}
}