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 ach
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
{
/// TargetType must be Visibility
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();
}
}