0

Is it possible to do something similar to this without introducing a new variable like

bool showsearch = !ShowUser;

Visibility="{Binding !ShowUser, Converter={StaticResource BoolToVis}}"

katie77
  • 1,707
  • 4
  • 24
  • 40

3 Answers3

1

You can do it using a style and a DataTrigger. Apply the style to the element in question, and provide 2 datatrigger values- one to set visibility to true, one to false. Similar to DataTrigger where value is NOT null?

Community
  • 1
  • 1
Chris Shain
  • 49,121
  • 5
  • 89
  • 122
0

I don't think it's possible, you have to create an inverted Visibility converter like this,

public class InvertedBooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool) value;
        }
        else if (value is bool?)
        {
            bool? nullable = (bool?) value;
            flag = nullable.Value;
        }
        return flag ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value is Visibility && (Visibility) value == Visibility.Collapsed;
    }
}
Justin XL
  • 37,545
  • 7
  • 79
  • 128
0

You can Write a Converter BoolToCollapsed Converter ForExample

public class BoolToVisibleConverter : ConverterMarkupExtension<BoolToVisibleConverter>, 
              IValueConverter
{
      public object Convert(object value, Type targetType, object parameter, 
                                CultureInfo culture)
            {
      bool obj=(bool) value;
      if(!obj)
      return Visibility.Collapsed;
      else
      return Visibility.Visible;

    }
     public object ConvertBack(object value, Type targetType, object parameter, 
             CultureInfo culture)
             {
               Visibility obj=(Visibility) value;
               if(obj==Visibility.Visible)
               return true;
               else
               return false;
             }

}

Add a Static Resource to Window.Resources

<Window.Resources>
    <converter:BoolToVisibleConverter x:Key="BoolToCollapsed"/>
</Window.Resources>

And then use it to a Grid,DockPanel Or anyother

 <DockPannel Visibility={Binding DockVisible, 
         Converter={StaticResource BoolToCollapsed}/>
Nivid Dholakia
  • 4,842
  • 4
  • 27
  • 55