|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BooleanToVisibilityConverter.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Convert from bool to <see cref="T:System.Windows.Visibility" /> and back.
// The bool value true will be converted to Visibility.Visible.
// The converted value for bool value false depends on the Visibility value given on construction of the object.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Data;
namespace Catel.Windows.Data.Converters
{
/// <summary>
/// Convert from bool to <see cref="T:System.Windows.Visibility" /> and back.
/// The bool value true will be converted to Visibility.Visible.
/// The bool value false will be converted to Visibility.Collapsed.
/// </summary>
#if !SILVERLIGHT
[ValueConversion(typeof(bool), typeof(Visibility))]
#endif
public class BooleanToCollapsingVisibilityConverter : VisibilityConverterBase
{
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="BooleanToCollapsingVisibilityConverter"/> class.
/// </summary>
public BooleanToCollapsingVisibilityConverter()
: base(Visibility.Collapsed) { }
/// <summary>
/// Initializes a new instance of the <see cref="BooleanToCollapsingVisibilityConverter"/> class.
/// </summary>
/// <param name="notVisibleVisibility">The <see cref="Visibility"/> state when not visibible should be returned.</param>
/// <exception cref="ArgumentException">when <paramref name="notVisibleVisibility"/> is <see cref="Visibility.Visible"/>.</exception>
internal BooleanToCollapsingVisibilityConverter(Visibility notVisibleVisibility)
: base(notVisibleVisibility) { }
#endregion
/// <summary>
/// Determines what value this converter should return.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// <c>true</c> if the specified value is visible; otherwise, <c>false</c>.
/// </returns>
public override bool IsVisible(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Get the parameter
bool? param = null;
bool tmp;
if (parameter != null && bool.TryParse(parameter.ToString(), out tmp))
{
param = tmp;
}
if (value is bool)
{
if (!param.HasValue)
{
// Convert the regular way
return (bool) value;
}
// Convert by comparing the value and the param
return ((bool)value == param.Value);
}
// Not visible by default
return false;
}
/// <summary>
/// Convert Visibility back to bool.
/// </summary>
/// <param name="value">A value. Only value of type <see cref="T:System.Windows.Visibility" /> is supported,</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>
/// When value is Visibility.Visible then true else false.
/// </returns>
public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Get the parameter
bool? param = null;
bool tmp;
if (parameter != null && bool.TryParse(parameter.ToString(), out tmp))
{
param = tmp;
}
if (value is Visibility)
{
bool isVisible = (Visibility)value == Visibility.Visible;
if (param.HasValue)
{
return isVisible == param;
}
return isVisible;
}
// Not visible
return false;
}
}
#if !SILVERLIGHT
/// <summary>
/// Convert from bool to <see cref="T:System.Windows.Visibility" /> and back.
/// The bool value true will be converted to Visibility.Visible.
/// The bool value false will be converted to Visibility.Hidden.
/// </summary>
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToHidingVisibilityConverter : BooleanToCollapsingVisibilityConverter
{
/// <summary>
/// Default constructor.
/// </summary>
public BooleanToHidingVisibilityConverter()
: base(Visibility.Hidden) { }
}
#endif
}
|
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.