Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight Bind to inverse of boolean property value

I want to bind a controls visibility to inverse of boolean property value. I have a property CanDownload, if it is true then I want to hide textbox and vice versa. How can I achieve this?

Thanks

like image 368
joblot Avatar asked May 28 '10 03:05

joblot


2 Answers

This sort of question is asked so often and the answers so similar I thought its time to have a single answer to all (ok may "most") of the bool to value conversion questions. I've blogged it here.

The code is quite simple so I'll paste it here too:-

public class BoolToValueConverter<T> : IValueConverter
{
    public T FalseValue { get; set; }
    public T TrueValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return FalseValue;
        else
            return (bool)value ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value != null ? value.Equals(TrueValue) : false;
    }
}

Now you can create converter to visibility with a one-liner:-

public class BoolToVisibilityConverter : BoolToValueConverter<Visibility> { }

Then for you can create an instance converter in a resource like this:-

<local:BoolToVisibilityConverter x:Key="InverseVisibility" TrueValue="Collapsed" FalseValue="Visible" />

Note the TrueValue and FalseValue are the swapped around from the more natural order giving you the inverted logic you wanted. With this in the Resources in your UserControl or even App.xaml you can now use it to bind to the CanDownload property to TextBox Visibility property:-

<TextBox Visibility="{Binding CanDownload, Converter={StaticResource InverseVisibility}}" />
like image 156
AnthonyWJones Avatar answered Oct 10 '22 14:10

AnthonyWJones


I use a BoolToVisibilityConverter that allows you to pass "Inverse" as the ConverterParameter to inverse the conversion and show only if the property is false.

public class BoolToVisibilityConverter : IValueConverter
{
    /// <exception cref="ArgumentException">TargetType must be Visibility</exception>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(!(value is bool))
            throw new ArgumentException("Source must be of type bool");

        if(targetType != typeof(Visibility))
            throw new ArgumentException("TargetType must be Visibility");

        bool v = (bool) value;

        if(parameter is string && parameter.ToString() == "Inverse")
            v = !v;

        if (v)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
like image 45
Stephan Avatar answered Oct 10 '22 15:10

Stephan