This is my recourse converter
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
And I have 2 buttons and 1 checkobox
the add button I want to show if the checkbox is checked and the edit button I want to show if the checkbox is not checked
<Button x:Name="Update" Content="שמור שינויים" Grid.Row="3" Height="30" Visibility="{Binding IsChecked ,TargetNullValue=true, ElementName=Checked, FallbackValue=Hidden, Converter={StaticResource BoolToVisConverter}}" Margin="10,23,10,22" Background="LightBlue" FontSize="15" Click="upDate_Click" />
<Button x:Name="Add" Content=" הוספה" Grid.Row="4" Height="30" Visibility="{Binding !IsChecked , FallbackValue=Visible, TargetNullValue=false, ElementName=Checked, Converter={StaticResource BoolToVisConverter}}" Margin="10,10,10,35" Background="LightBlue" FontSize="15" Click="Add_Click" />
<CheckBox x:Name="Checked" IsChecked="True" />
but {Binding !IsChecked Not work
How can I hide element if checked?
Try use this converter:
public class InvertedBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool boolValue) {
return !boolValue ? Visibility.Visible : Visibility.Collapsed;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and in your xaml.cs:
<Button x:Name="Add" Content=" הוספה" Grid.Row="4" Height="30"
Visibility="{Binding IsChecked, ElementName=Checked, Converter={StaticResource InvertedBoolToVisConverter}}"
Margin="10,10,10,35" Background="LightBlue" FontSize="15" />
do not forget to include the converter in the resources:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
<local:InvertedBooleanToVisibilityConverter x:Key="InvertedBoolToVisConverter" />
</Window.Resources>