3

It is possible within XAML binding markup to negate a Boolean property. Not sure if that is the correct description. For example, I am using one of the built in converters for setting the Visibility of a window border control based on if it is active or not.

<Border BorderBrush="{StaticResource BorderColorBrush}" 
        BorderThickness="1" 
        Visibility="{Binding IsActive, 
                     RelativeSource={RelativeSource FindAncestor, 
                                     AncestorType={x:Type Window}}, 
                     Converter={StaticResource bool2VisibilityConverter}}" />

What I want to the opposite of this, I would like the Visibility to be set to false if the Window is active. This is just an example, but I have run across other situations where it would be nice to apply a ‘!’ to a Boolean property that is being evaluated by a stock converter so I do not have to write a custom one.

Rob Goodwin
  • 2,676
  • 2
  • 27
  • 47
  • Does this answer your question? [How to bind inverse boolean properties in WPF?](https://stackoverflow.com/questions/1039636/how-to-bind-inverse-boolean-properties-in-wpf) – StayOnTarget May 01 '20 at 20:15

2 Answers2

7

An alternative would be to use a DataTrigger instead of a binding converter:

<Border BorderBrush="{StaticResource BorderColorBrush}" 
        BorderThickness="1">
    <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive,
                                       RelativeSource={RelativeSource FindAncestor,
                                                       AncestorType=Window}}"
                             Value="True">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • This was exactly what I was looking for. I think I can replace some of my simple converters with a bit of this markup. I don't dislike the converter as they have there place and I make use of them, but this will come in handy in a number of areas. Learning WPF has been like peeling the onion. I love it and keep learning something new every day. – Rob Goodwin Aug 22 '13 at 17:05
3

Either create a negated property in the ViewModel:

public bool IsNotActive {get { return !IsActive; } }

And notify change accordingly,

-- OR --

use an InverseBoolToVisibilityConverter:

public class InverseBoolToVisibilityConverter: BaseConverterMarkupExtension<bool, Visibility>
    {
        public override Visibility Convert(bool value, Type targetType, object parameter)
        {
            return !value ? Visibility.Visible : parameter != null ? Visibility.Hidden : Visibility.Collapsed;
        }

        public override bool ConvertBack(Visibility value, Type targetType, object parameter)
        {
            return value != Visibility.Visible;
        }
    }
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • 1
    Or enhance the converter to accept a parameter you can receive via ConverterParameter - if it's "not" or something like that negate the boolean you return. Not really discoverable or very intuitive but works like a charm. – Alex Paven Aug 22 '13 at 16:19