Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to bind inverse boolean properties? - [Answered]

How to bind inverse boolean properties? - [Answered]

Scheduled Pinned Locked Moved WPF
questioncsharpdatabasewpfwcf
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alisaunder
    wrote on last edited by
    #1

    Let me clarify that a bit more. I'm currently binding my buttons to be enabled if my Stored settings bool value is true. Meaning in this case dbOpen. This is true if my database is loaded, false if it's closed. Now I have certain buttons I want to be enabled when this is true so I have the xml code like this:

    IsEnabled="{Binding Source={x:Static prop:Settings.Default}, Path=dbOpen, Mode=TwoWay}"

    Now I want certain buttons to be able to be disabled when the dbOpen is true so how do I flip the value in the xml statement? in c# it would be like : If (Settings.Default.dbOpen) to check if it's not true you would If(!Settings.Default.dbopen)

    M 1 Reply Last reply
    0
    • A Alisaunder

      Let me clarify that a bit more. I'm currently binding my buttons to be enabled if my Stored settings bool value is true. Meaning in this case dbOpen. This is true if my database is loaded, false if it's closed. Now I have certain buttons I want to be enabled when this is true so I have the xml code like this:

      IsEnabled="{Binding Source={x:Static prop:Settings.Default}, Path=dbOpen, Mode=TwoWay}"

      Now I want certain buttons to be able to be disabled when the dbOpen is true so how do I flip the value in the xml statement? in c# it would be like : If (Settings.Default.dbOpen) to check if it's not true you would If(!Settings.Default.dbopen)

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I have a set of NOT converters!

      public class BooleanNotConverter : IValueConverter
      {
      	object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
      	{
      		bool bReturn = false;
      		int iValue;
      		try
      		{
      			if (Int32.TryParse(value.ToString(), out iValue))
      			{
      				bReturn = iValue != 0;
      			}
      			else
      			{
      				Boolean.TryParse(value.ToString(), out bReturn);
      			}
      		}
      		catch { bReturn = false; }
      		return !bReturn;
      	}
      
      	object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      	{
      		int iReturn = 0;
      
      		bool bReturn = false;
      		Boolean.TryParse(value.ToString(), out bReturn);
      		if (!bReturn)
      		{ iReturn = -1; }
      
      		return iReturn;
      	}
      }
      

      And the xaml

      IsEnabled="{Binding IsLocked,Converter={StaticResource BooleanNotConverter}}"

      And the App.xaml resource

      Never underestimate the power of human stupidity RAH

      A S 2 Replies Last reply
      0
      • M Mycroft Holmes

        I have a set of NOT converters!

        public class BooleanNotConverter : IValueConverter
        {
        	object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        	{
        		bool bReturn = false;
        		int iValue;
        		try
        		{
        			if (Int32.TryParse(value.ToString(), out iValue))
        			{
        				bReturn = iValue != 0;
        			}
        			else
        			{
        				Boolean.TryParse(value.ToString(), out bReturn);
        			}
        		}
        		catch { bReturn = false; }
        		return !bReturn;
        	}
        
        	object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        	{
        		int iReturn = 0;
        
        		bool bReturn = false;
        		Boolean.TryParse(value.ToString(), out bReturn);
        		if (!bReturn)
        		{ iReturn = -1; }
        
        		return iReturn;
        	}
        }
        

        And the xaml

        IsEnabled="{Binding IsLocked,Converter={StaticResource BooleanNotConverter}}"

        And the App.xaml resource

        Never underestimate the power of human stupidity RAH

        A Offline
        A Offline
        Alisaunder
        wrote on last edited by
        #3

        For some reason it's just not working as expected. The open database button is enabled by default this allows it to open a database. Once I load the database I set the dbOpen property to True which if this is working properly should disable the button since it inverts the True to a false. But it's not. The Button is enabled the entire time. Now I know dbOpen is true because my code to load the database will only run if(!System.Properties.Default.dbOpen), and while the button does not disable the code to open the database does not fire when clicked.

        M 1 Reply Last reply
        0
        • A Alisaunder

          For some reason it's just not working as expected. The open database button is enabled by default this allows it to open a database. Once I load the database I set the dbOpen property to True which if this is working properly should disable the button since it inverts the True to a false. But it's not. The Button is enabled the entire time. Now I know dbOpen is true because my code to load the database will only run if(!System.Properties.Default.dbOpen), and while the button does not disable the code to open the database does not fire when clicked.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I know this is a really dumb suggestion but try it any way, presuming your converter is working as expected and have tested it on some other control, try changing your button from a Command"" to Click. I have found (twice) that for some reason the IsEnabled event is ignored if there is a command on the button (I have no idea why and don't have the motivation to chase it down as it happens only rarely).

          Never underestimate the power of human stupidity RAH

          A 2 Replies Last reply
          0
          • M Mycroft Holmes

            I know this is a really dumb suggestion but try it any way, presuming your converter is working as expected and have tested it on some other control, try changing your button from a Command"" to Click. I have found (twice) that for some reason the IsEnabled event is ignored if there is a command on the button (I have no idea why and don't have the motivation to chase it down as it happens only rarely).

            Never underestimate the power of human stupidity RAH

            A Offline
            A Offline
            Alisaunder
            wrote on last edited by
            #5

            I tried that and sadly it didn't change anything. One thing is possible I'm using the Actipro WPF Ribbon and it's the QAT open button, and backstage open buttons I'm trying to disable using this method. I don't know if by chance it's an issue with their control or not. I can tell you that I have had success using a regular boolean converter to enable/disable the buttons on their ribbon, just not this inverted boolean converter.

            1 Reply Last reply
            0
            • M Mycroft Holmes

              I know this is a really dumb suggestion but try it any way, presuming your converter is working as expected and have tested it on some other control, try changing your button from a Command"" to Click. I have found (twice) that for some reason the IsEnabled event is ignored if there is a command on the button (I have no idea why and don't have the motivation to chase it down as it happens only rarely).

              Never underestimate the power of human stupidity RAH

              A Offline
              A Offline
              Alisaunder
              wrote on last edited by
              #6

              Got it working! Was a really stupid mistake I forgot to tell it the source of where my variable to tie it too was. Since it's a Property stored by the project I had to do the IsEnabled command as so:

              IsEnabled="{Binding Source={x:Static prop:Settings.Default}, Path=dbOpen, Converter={StaticResource booleanNotConverter}}"

              1 Reply Last reply
              0
              • M Mycroft Holmes

                I have a set of NOT converters!

                public class BooleanNotConverter : IValueConverter
                {
                	object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
                	{
                		bool bReturn = false;
                		int iValue;
                		try
                		{
                			if (Int32.TryParse(value.ToString(), out iValue))
                			{
                				bReturn = iValue != 0;
                			}
                			else
                			{
                				Boolean.TryParse(value.ToString(), out bReturn);
                			}
                		}
                		catch { bReturn = false; }
                		return !bReturn;
                	}
                
                	object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                	{
                		int iReturn = 0;
                
                		bool bReturn = false;
                		Boolean.TryParse(value.ToString(), out bReturn);
                		if (!bReturn)
                		{ iReturn = -1; }
                
                		return iReturn;
                	}
                }
                

                And the xaml

                IsEnabled="{Binding IsLocked,Converter={StaticResource BooleanNotConverter}}"

                And the App.xaml resource

                Never underestimate the power of human stupidity RAH

                S Offline
                S Offline
                SledgeHammer01
                wrote on last edited by
                #7

                While your code works just fine... a more generic solution is to use a map converter. That way you don't need to keep writing these one-off converters that basically map one value to another. It's all in one converter and you just define the mapping in XAML.

                M 1 Reply Last reply
                0
                • S SledgeHammer01

                  While your code works just fine... a more generic solution is to use a map converter. That way you don't need to keep writing these one-off converters that basically map one value to another. It's all in one converter and you just define the mapping in XAML.

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  Well don't just drop it as a comment, link please. I have about 8 converters so there is very limited rewriting.

                  Never underestimate the power of human stupidity RAH

                  S 1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    Well don't just drop it as a comment, link please. I have about 8 converters so there is very limited rewriting.

                    Never underestimate the power of human stupidity RAH

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #9

                    :). Ok... It's part of this project: http://wpfconverters.codeplex.com/[^] Like I said, nothing wrong with your code. Just that this converter allowed me to get rid of about 73 others. You can map any value you can reference in XAML to any other value. I use it for bool inversion... I also use it in a clever way :) to allow for triggering on empty collections. I map count=0 to true and then use the fallback value (for any other count) = false.

                    M 1 Reply Last reply
                    0
                    • S SledgeHammer01

                      :). Ok... It's part of this project: http://wpfconverters.codeplex.com/[^] Like I said, nothing wrong with your code. Just that this converter allowed me to get rid of about 73 others. You can map any value you can reference in XAML to any other value. I use it for bool inversion... I also use it in a clever way :) to allow for triggering on empty collections. I map count=0 to true and then use the fallback value (for any other count) = false.

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #10

                      Thanks for that, something for the weekend experimentation!

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups