0

I want to bind a single property to two buttons for Visiblity. I am using BooleantoVisibility Converter. I am able to hide or show button based on the Property value. My issue is, I want to show only one of two buttons. Below is my Code. Is there a way to bind using "NOT" or I have to create a new Property ?

 <telerik:RadButton Content="Close" x:Name="btnClose" Visibility="{Binding Path=IsNewRecord, Converter={StaticResource BoolToVisiblity}}" Command="{Binding CloseCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>
 <telerik:RadButton Content="Delete" x:Name="btnDelete" Visibility="{Binding Path=IsNewRecord, Converter={StaticResource BoolToVisiblity}}" Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>
Chatra
  • 2,989
  • 7
  • 40
  • 73
  • @SteveMitcham BooleanVisibilityConverter is inbuilt. There is no need to write that BooleanVisibilityConverter class seperately. But I think we have to write Invert class. – Chatra Mar 30 '15 at 15:08

1 Answers1

2

Create an InvertBooleanToVisibility Converter. Bind same property to both converters.

public class InvertBooleanToVisibilityConverter : IValueConverter
{       
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var boolValue = (bool)value;            

        return boolValue ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

In XAML

<UserControl.Resources>
  <Converters:InvertBooleanToVisibilityConverter x:Key="InvertConverter"/>
</UserControl.Resources>

<telerik:RadButton Content="Delete" x:Name="btnDelete" 
                   Visibility="{Binding Path=IsNewRecord, Converter={StaticResource InvertConverter}}"
                   Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208