1

In WPF, I have an element whose visibility should be bound to a checkbox. I can use BindingPath=IsChecked and Converter={StaticResource convVisibility} to make it visible. However, I want to make it hidden and there is no BindingPath=IsUnchecked. Can I invert the value of the boolean to visibility converter? Thanks for help.

This is in the window xaml:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="convVisibility"/>
</Window.Resources>
Clemens
  • 123,504
  • 12
  • 155
  • 268
Buck
  • 37
  • 6

1 Answers1

2

This is my solution of this problem:

public class BoolVisibilityCollapsedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool param = true;

            if (parameter != null)
                param = System.Convert.ToBoolean(parameter);

            bool state = (bool)value;

            Visibility visibility = Visibility.Visible;

            if (state != param)
                visibility = Visibility.Collapsed;

            return visibility;
        }

If you use this converter, you will switch visibilty like True = Visible / False = Collapsed. If you want the different behavior (True = Collapsed / False = Visible) just use CommandParameter="False"

Miroslav Endyš
  • 154
  • 1
  • 8