This is a simple MarkupExtension for Xamarin.Forms that imitates an if-else-statement. It was inspired by Thomas Levesque on https://stackoverflow.com/a/841894/6884587
Usage:
- Include xmlns:ext="clr-namespace:Xamarin.Forms.Extensions"
- Use on a bindable property: <Label Text="{ext:SwitchBinding IsLoggedIn, True=Log out, False=Log in}" />
You can also add a StringFormat: <Label Text="{ext:SwitchBinding IsLoggedIn, StringFormat='Action: {0}', True=Log out, False=Log in}" />
You can even add your own Converter and ConverterParameter within the markup, which get applied after the switch condition has been evaluated.
It is also possible to use this extension as an inverter like: <Label IsVisible="{ext:SwitchBinding IsLoggedIn, True=False, False=True}" />
The extension supports - identically to standard binding - the Path and Source property, where Path is set as the content property also like in the standard binding.
Prepared but not working is the feature to also bind the True and False property to a Property in your ViewModel. But this is not working due to a bug (True and False property always get assigned null):
https://bugzilla.xamarin.com/show_bug.cgi?id=25189 https://stackoverflow.com/questions/42648334/xamarin-forms-imarkupextension-with-bindable-property-does-not-work
So this is currently NOT working: <Label Text="{ext:SwitchBinding IsLoggedIn, True={Binding LogoutString}, False={Binding LoginString}}" />