Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF BooleanToVisibilityConverter that converts to Hidden instead of Collapsed when false?

Is there a way to use the existing WPF BooleanToVisibilityConverter converter but have False values convert to Hidden instead of the default Collapsed, or should I just write my own? I'm on a project where it's tremendous overhead to do something simple like this (shared stuff goes into a separate solution, and the rebuild/checkin/merge process is an overgrown mutated behemoth of a process), so I'd prefer if I could just pass a parameter to the existing one than to jump through the hoops just mentioned.

like image 400
Rich Avatar asked Jun 27 '10 16:06

Rich


3 Answers

I've found the simplest and best solution to be this:

[ValueConversion(typeof(bool), typeof(Visibility))] public sealed class BoolToVisibilityConverter : IValueConverter {   public Visibility TrueValue { get; set; }   public Visibility FalseValue { get; set; }    public BoolToVisibilityConverter()   {     // set defaults     TrueValue = Visibility.Visible;     FalseValue = Visibility.Collapsed;   }    public object Convert(object value, Type targetType,        object parameter, CultureInfo culture)   {     if (!(value is bool))       return null;     return (bool)value ? TrueValue : FalseValue;       }    public object ConvertBack(object value, Type targetType,        object parameter, CultureInfo culture)   {     if (Equals(value, TrueValue))       return true;     if (Equals(value, FalseValue))       return false;     return null;   } } 

When using it, just configure a version that does exactly what you need it to in XAML like this:

<Blah.Resources>   <local:BoolToVisibilityConverter          x:Key="BoolToHiddenConverter"          TrueValue="Visible" FalseValue="Hidden" /> </Blah.Resources> 

Then use it in one or more bindings like this:

<Foo Visibility="{Binding IsItFridayAlready,                            Converter={StaticResource BoolToHiddenConverter}}" /> 

This simple solution addresses hidden/collapsed preferences as well as reversing/negating the effect.

SILVERLIGHT USERS must drop the [ValueConversion] declaration as that attribute is not part of the Silverlight framework. It's not strictly needed in WPF either, but is consistent with built-in converters.

like image 150
Drew Noakes Avatar answered Oct 01 '22 22:10

Drew Noakes


Unfortunately, it only converts to Visible or Collapsed, so you'll have to write your own. Here is the Convert method according to Reflector:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {     bool flag = false;     if (value is bool)     {         flag = (bool)value;     }     else if (value is bool?)     {         bool? nullable = (bool?)value;         flag = nullable.HasValue ? nullable.Value : false;     }     return (flag ? Visibility.Visible : Visibility.Collapsed); } 
like image 31
Quartermeister Avatar answered Oct 01 '22 21:10

Quartermeister


Can you not just use a style instead of a converter? The code would be something like:

<Style x:Key="Triggers" TargetType="Button">
    <Style.Triggers>
    <Trigger Property="{Binding ...}" Value="false">
        <Setter Property = "Visibility" Value="Hidden"/>
    </Trigger>
    </Style.Triggers>
</Style>

You'll need to provide the property binding yourself to point to your bool property.

like image 40
cristobalito Avatar answered Oct 01 '22 22:10

cristobalito