Reputation: 2777
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.
Upvotes: 3
Views: 5854
Reputation: 128098
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>
Upvotes: 7
Reputation: 44048
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;
}
}
Upvotes: 3