none
Invert BooleanToVisibilityConverter?

    Question

  • Hi there,

    Is there an easy way to invert the BooleanToVisiblityConverter?

    I need this in a project where i have a toolbar and 5 tab pages, on the toolbar i want one button to be INvisible if tab page 3 is selected..

    so i need the button to be visible if tab page 3 is NOT selected, but sadly there is no IsNOTSelected property Smile


    Thanks for answers, regards
    bgreber
    Thursday, September 06, 2007 8:56 AM

Answers

  • Hey, this is my 100th post!  (Guess I'll be done now!  )

     

    I use the following simple converter class in a lot of my projects for the different translations between bool and visibility.  The different variants of the converter can be declared in XAML as follows:

     

    Code Snippet

     

    <src:VisToBool x:Key="TrueIfVisible" Inverted="False" Not="False" />

    <src:VisToBool x:Key="TrueIfNotVisible" Inverted="False" Not="True" />

    <src:VisToBool x:Key="VisibleIfTrue" Inverted="True" Not="False" />

    <src:VisToBool x:Key="VisibleIfNotTrue" Inverted="True" Not="True" />

     

     

    And here is the code:

     

    Code Snippet

     

    public class VisToBool : IValueConverter

    {

        private bool _inverted = false;

        public bool Inverted

        {

            get { return _inverted; }

            set { _inverted = value; }

        }

     

        private bool _not = false;

        public bool Not

        {

            get { return _not; }

            set { _not = value; }

        }

     

        private object VisibilityToBool(object value)

        {

            if (!(value is Visibility))

                return DependencyProperty.UnsetValue;

     

            return (((Visibility)value) == Visibility.Visible) ^ Not;

        }

     

        private object BoolToVisibility(object value)

        {

            if (!(value is bool))

                return DependencyProperty.UnsetValue;

     

            return ((bool)value ^ Not) ? Visibility.Visible

                : Visibility.Collapsed;

        }

     

        public object Convert(object value, Type targetType,

                object parameter, CultureInfo culture)

        {

            return Inverted ? BoolToVisibility(value)

                : VisibilityToBool(value);

        }

     

        public object ConvertBack(object value, Type targetType,

                object parameter, CultureInfo culture)

        {

            return Inverted ? VisibilityToBool(value)

                : BoolToVisibility(value);

        }

    }

     

     

     

    Thursday, September 06, 2007 7:37 PM
  • 
    
    <Button.Style>
    <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
    <Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=tab3, Path=IsSelected}" Value="True">
    <Setter Property="Button.IsVisible" Value="False" />
    </DataTrigger>
     
    <DataTrigger Binding="{Binding ElementName=tab3, Path=IsSelected}" Value="False">
    <Setter Property="Button.IsVisible" Value="True" />
    </DataTrigger>

    </Style.Triggers>
    </Style>
    </Button.Style>

    Thursday, September 06, 2007 9:15 AM
  •  

    your own implementaiton of IValueConverter would be the way to go i think. Although the xaml way above is pretty nice too.

     

    Alternatively you might want to use a RoutedCommand instead of the button/click (or whatever) event because then you would have the CanExecute property which you could set based on which tab was active. If you further wanted the control invisible you could bind it's visibility to it's Command's Can Execute Property.

     

     

    Thursday, September 06, 2007 6:04 PM
  • If you are using this as a one-time deal then i would go with Frances83's solution. But if you plan on using it throughout your project I would go with Dr. WPF's solution (Very nice converters!).
    Friday, September 07, 2007 1:15 AM

All replies

  • 
    
    <Button.Style>
    <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
    <Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=tab3, Path=IsSelected}" Value="True">
    <Setter Property="Button.IsVisible" Value="False" />
    </DataTrigger>
     
    <DataTrigger Binding="{Binding ElementName=tab3, Path=IsSelected}" Value="False">
    <Setter Property="Button.IsVisible" Value="True" />
    </DataTrigger>

    </Style.Triggers>
    </Style>
    </Button.Style>

    Thursday, September 06, 2007 9:15 AM
  •  

    your own implementaiton of IValueConverter would be the way to go i think. Although the xaml way above is pretty nice too.

     

    Alternatively you might want to use a RoutedCommand instead of the button/click (or whatever) event because then you would have the CanExecute property which you could set based on which tab was active. If you further wanted the control invisible you could bind it's visibility to it's Command's Can Execute Property.

     

     

    Thursday, September 06, 2007 6:04 PM
  • Hey, this is my 100th post!  (Guess I'll be done now!  )

     

    I use the following simple converter class in a lot of my projects for the different translations between bool and visibility.  The different variants of the converter can be declared in XAML as follows:

     

    Code Snippet

     

    <src:VisToBool x:Key="TrueIfVisible" Inverted="False" Not="False" />

    <src:VisToBool x:Key="TrueIfNotVisible" Inverted="False" Not="True" />

    <src:VisToBool x:Key="VisibleIfTrue" Inverted="True" Not="False" />

    <src:VisToBool x:Key="VisibleIfNotTrue" Inverted="True" Not="True" />

     

     

    And here is the code:

     

    Code Snippet

     

    public class VisToBool : IValueConverter

    {

        private bool _inverted = false;

        public bool Inverted

        {

            get { return _inverted; }

            set { _inverted = value; }

        }

     

        private bool _not = false;

        public bool Not

        {

            get { return _not; }

            set { _not = value; }

        }

     

        private object VisibilityToBool(object value)

        {

            if (!(value is Visibility))

                return DependencyProperty.UnsetValue;

     

            return (((Visibility)value) == Visibility.Visible) ^ Not;

        }

     

        private object BoolToVisibility(object value)

        {

            if (!(value is bool))

                return DependencyProperty.UnsetValue;

     

            return ((bool)value ^ Not) ? Visibility.Visible

                : Visibility.Collapsed;

        }

     

        public object Convert(object value, Type targetType,

                object parameter, CultureInfo culture)

        {

            return Inverted ? BoolToVisibility(value)

                : VisibilityToBool(value);

        }

     

        public object ConvertBack(object value, Type targetType,

                object parameter, CultureInfo culture)

        {

            return Inverted ? VisibilityToBool(value)

                : BoolToVisibility(value);

        }

    }

     

     

     

    Thursday, September 06, 2007 7:37 PM
  • If you are using this as a one-time deal then i would go with Frances83's solution. But if you plan on using it throughout your project I would go with Dr. WPF's solution (Very nice converters!).
    Friday, September 07, 2007 1:15 AM
  • here is my approach

    <local:BooleanToVisibilityConverter x:Key="visibilityConverter" OnTrue="Visible" OnFalse="Collapsed" />

    you can get any combination you want now.

    leblanc
    • Proposed as answer by kainhart Tuesday, January 26, 2010 4:41 PM
    Monday, September 29, 2008 4:05 PM
  • Hi, I am trying to do the same but get this error -

    Error 1 'Setter' object cannot specify value for read-only property 'IsVisible'. E:\MyFile.xaml 115 17

    XAML -
    <Button.Style>
        <Style
            BasedOn="{StaticResource {x:Type Button}}"
            TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Path=IsNewProfile}"
                    Value="True">
                    <Setter
                        Property="Button.IsVisible"
                        Value="False" />
                </DataTrigger>
                <DataTrigger
                    Binding="{Binding Path=IsNewProfile}"
                    Value="False">
                    <Setter
                        Property="Button.IsVisible"
                        Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    


    Cheerio

    • Edited by akjoshi Monday, April 20, 2009 12:28 PM Added missing code
    Monday, April 20, 2009 12:23 PM
  • Hi, I am trying to do the same but get this error -

    Error 1 'Setter' object cannot specify value for read-only property 'IsVisible'. E:\MyFile.xaml 115 17

    XAML -
    <Button.Style>
        <Style
            BasedOn="{StaticResource {x:Type Button}}"
            TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Path=IsNewProfile}"
                    Value="True">
                    <Setter
                        Property="Button.IsVisible"
                        Value="False" />
                </DataTrigger>
                <DataTrigger
                    Binding="{Binding Path=IsNewProfile}"
                    Value="False">
                    <Setter
                        Property="Button.IsVisible"
                        Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    
    

     


    Cheerio

     


    Use the Property "Visibility" and assign a value of Visible, Hidden, or Collapsed.


    Wednesday, August 05, 2009 4:37 PM