Search code examples
xamarinxamarin.formsmvvmcross

Show custom fields based on user input in Xamarin Forms with MvvmCross


I'm working on a Xamarin.Forms project with MvvmCross. In one screen, I have a combo box to choose a field type: string, number, list or date. When a type is chosen, I have to show a custom input for the selected type: if it's string, I have to show a normal Entry, if it's Date, a DatePicker, etc.

How I can do that?


Solution

    1. Add in your ViewModel property FieldType.
    2. Bind it to all types of inputs like this:
      IsVisible="{Binding FieldType, Converter={StaticResource IsEqualConverter}, ConverterParameter=current_field_type}"

    3. Create IsEqualConverter and inside compare FieldType with parameter.

    It will show and hide input fields based on selected FieldType. You can read more about converters here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters