views:

296

answers:

3

I have a (varchar) field Foo which can only be specified if (bit) Bar is not true. I would like the textbox in which Foo is displayed to be disabled when Bar is true -- essentially, FooBox.Enabled = !isBar. I'm trying to do something like

FooBox.DataBindings.Add(new Binding("Enabled", source, "!isBar"));

but of course the bang in there throws an exception. I've also tried constructs like "isBar != true" or "isBar <> true", but none work. Am I barking up the wrong tree here?

+1  A: 

As far as I can tell, Databind uses reflection to find the member passed as the 3rd string argument. You cannot pass an expression there, just the member name.

FlySwat
I think Jake has the general idea (making a Property that calculates what you want) but it still feels a bit hacky.
Coderer
A: 

if isBar is a property of the source class (otherwise you need a property of a class to do the binding) this should work:

FooBox.DataBindings.Add("Enabled", source, "isBar");

but remember that source.isBar must exist and be a boolean.

gcores
I understand the basic concept, it's just that it seems kind of limited. I was hoping you'd have a little more flexibility.
Coderer
+1  A: 

I tried doing something like this a while ago and the best I could come up with was either

a) Changing the source class to also have a NotBar property and bind to that

b) Make a dumb wrapper class around source that has a NotBar property and bind to that.

Jacob Adams
I had an idea sort of like this, but I figured it *should* be possible without resorting to this sort of hack. Apparently not?
Coderer
I agree that it's a hack. I was quite disappointed by it as well. For my project I basically resorted to making my own system that would handle the the source changing and change the control accordingly. I haven't looked at it much, but WPF databinding is much more advanced, if that's an option.
Jacob Adams
I'm going to look into WPF when I get a chance. Sadly, the project I'm asking about is just about in the can, so a switch is not an option at this point. But I may transition before my next one...
Coderer
FYI, I wound up using this as an answer to one of my other questions (about radio buttons). Thanks!
Coderer