2

It's really common the scenario where you need to negate a value that comes from the ViewModel. We end up using a Converter like the so called "InverseBoolConverter".

My question is: is there a handy way to avoid using a Converter that doesn't involve changing the ViewModel?

NOTE: I'm using ReactiveUI

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • Instead of negating the input could you negate the thing you are testing with in your logic? For example swapping a `Value=True` to a `Value=False` in your conditional. – Scott Chamberlain Jan 26 '16 at 15:55
  • 2
    I don't think there are less intrusive ways than using a converter, other than adding an inverted property to your view model. I usually put a boolean inverter converter in a resource dictionary in `App.xaml` so it's available in all windows and views. – Pieter Witvoet Jan 26 '16 at 15:57
  • @ScottChamberlain it's OK, but it adds boilerplate to your classes. – SuperJMN Jan 26 '16 at 22:02
  • @PieterWitvoet The bad thing is that it's so common that you end up create the same converters again and again: BoolToVisibility, BoolToVisibilityInverse, NullToVisibility... and a huge bunch that make bindings a bit less legible. – SuperJMN Jan 26 '16 at 22:04
  • @SuperJMN: It's not so bad when you make your converters configurable by adding some properties to them, for example: ``, where `True` and `False` are properties of type `Visibility`. That makes it easy to create an inverse visibility converter without having to write yet another converter class. For `null` you can set the `TargetNullValue` property of a binding. – Pieter Witvoet Jan 27 '16 at 07:55
  • FWIW, there will be an overload of `Bind` accepting two functions providing transitions one way and another in RxUI 7.0 (see https://github.com/reactiveui/ReactiveUI/issues/513). For now, I guess you have to implement [`IBindingTypeConverter` yourself](https://github.com/reactiveui/ReactiveUI/blob/38f34d06ab3144d66ca64329a719944d6a294cd3/ReactiveUI/RegisterableInterfaces.cs#L153) – pmbanka Jan 27 '16 at 12:42

1 Answers1

5

If you need one way binding only, you can do:

this.OneWayBind(ViewModel, model => model.BoolProperty, view => view.BoolProperty, boolValue => !boolValue);
ds-b
  • 361
  • 1
  • 5