How to bind inverse boolean properties? - [Answered]
-
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)
-
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)
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
-
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
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.
-
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.
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
-
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
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.
-
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
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}}"
-
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
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.
-
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.
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
-
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
:). 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.
-
:). 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.
Thanks for that, something for the weekend experimentation!
Never underestimate the power of human stupidity RAH