Skip to main content

Add Editors at Runtime

This example demonstrates how to add editors to a data form at runtime. It adds a DataFormTextItem object to the data form’s Items collection when a user taps the Add Editor button.

View Example

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:dxdf="http://schemas.devexpress.com/xamarin/2014/forms/dataform"
    xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
    ios:Page.UseSafeArea="True"
    x:Class="DataForms_DynamicEditors.MainPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <dxdf:DataFormView x:Name="dataForm" />
        <Button Grid.Row="1" HeightRequest="50" Text="Add Editor" Clicked="Button_Clicked"/>
    </Grid>
</ContentPage>
using System.Collections.Generic;
using DevExpress.XamarinForms.DataForm;
using Xamarin.Forms;
using System;

namespace DataForms_DynamicEditors {
    public partial class MainPage : ContentPage {
        Dictionary<string, object> dataModel = new Dictionary<string, object>();
        public MainPage() {
            DevExpress.XamarinForms.DataForm.Initializer.Init();
            InitializeComponent();

            dataForm.IsAutoGenerationEnabled = false;
            dataForm.CommitMode = CommitMode.PropertyChanged;
            dataForm.DataObject = dataModel;
        }

        private void Button_Clicked(object sender, EventArgs e) {
            string editorName = $"Email {dataModel.Count}";
            object editorValue = "Test";

            var dataformItem = new DataFormTextItem { FieldName = editorName };

            dataModel.Add(dataformItem.FieldName, editorValue);
            dataForm.Items.Add(dataformItem);
        }
    }
}