Advertisement
is_a_cdr

BoolConverter.cs

May 16th, 2012
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1.     /// <summary>
  2.     /// Converts boolean values to arbitrary objects.
  3.     /// </summary>
  4.     /// <example>
  5.     /// Converting view model's boolean property to control visibility:
  6.     /// <Window.Resources>
  7.     ///     <infra:BoolConverter x:Key="falseHide" TrueValue="{x:Static sw:Visibility.Visible}" FalseValue="{x:Static sw:Visibility.Hidden}"/>
  8.     /// ...and then...
  9.     /// <SomeControl Visibility="{Binding ViewModelBooleanProperty, Converter={StaticResource falseHide}}">
  10.     /// </example>
  11.     class BoolConverter : IValueConverter
  12.     {
  13.         public object TrueValue { get; set; }
  14.         public object FalseValue { get; set; }
  15.         public object NullValue { get; set; }
  16.  
  17.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  18.         {
  19.             if (!(value is bool))
  20.                 return NullValue;
  21.             var boolValue = (bool)value;
  22.             return boolValue ? TrueValue : FalseValue;
  23.         }
  24.  
  25.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  26.         {
  27.             throw new NotImplementedException();
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement