1

how do i do something like this

<BooleanToVisibilityConverter x:Key="BoolToVis"/>

<WrapPanel>
     <TextBlock Text="{Binding ElementName=ConnectionInformation_ServerName,Path=Text}"/>
     <Image Source="Images/Icons/Select.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=true}"/>
     <Image Source="Images/Icons/alarm private.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=false}"/>
</WrapPanel>

is there a way to use the Boolean to vis converter but inverted without writing a whole method in C to do it? or should i just have these images overlap and hide one when i need to ?

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
New Bee
  • 991
  • 1
  • 13
  • 24
  • of course you can easily alter the converter to accept boolean parameter indicating whether it do standard conversion or inverted conversion. – har07 Feb 22 '14 at 07:12
  • Does this answer your question? [How do I invert BooleanToVisibilityConverter?](https://stackoverflow.com/questions/534575/how-do-i-invert-booleantovisibilityconverter) – StayOnTarget May 01 '20 at 20:14

2 Answers2

8

As far as I know, you have to write your own implementation for this. Here's what I use:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool boolValue = (bool)value;
        boolValue = (parameter != null) ? !boolValue : boolValue;
        return boolValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And I generally set ConverterParameter='negate' so it's clear in code what the parameter is doing. Not specifying a ConverterParameter makes the converter behave like the built-in BooleanToVisibilityConverter. If you want your usage to work, you can, of course, parse the ConverterParameter using bool.TryParse() and react to it.

Graham
  • 7,431
  • 18
  • 59
  • 84
K Mehta
  • 10,323
  • 4
  • 46
  • 76
  • 1
    Thanks for the response. it is correct so i have marked it as so. its just a shame there isnt an inbuilt paramater for such an action. thank you for your response. – New Bee Feb 23 '14 at 06:35
1

From @K Mehta (https://stackoverflow.com/a/21951103/1963978), with slight updates for the method signature for Windows 10 Universal applications (Changing from "CultureInfo culture" to "string language", per https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh701934.aspx) :

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, string language)
    {
        bool boolValue = (bool)value;
        boolValue = (parameter != null) ? !boolValue : boolValue;
        return boolValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
Community
  • 1
  • 1
matthewsheets
  • 4,535
  • 2
  • 15
  • 11