New to Telerik UI for .NET MAUIStart a free 30-day trial

Setting Editor Value Programmatically in DataForm for UI for .NET MAUI

Updated over 6 months ago

Environment

VersionProductAuthor
11.0.1DataForm for .NET MAUIDobrinka Yordanova

Description

I want to programmatically set the value of a specific editor in the DataForm for UI for .NET MAUI. My editors are generated using the OnEditorGenerated event.

This knowledge base article also answers the following questions:

  • How can I set a value to a DataForm editor programmatically?
  • How to use the EditorGenerated event in DataForm for UI for .NET MAUI?
  • How to bind model properties to DataForm editors?

Solution

To programmatically set the value of a specific editor in the DataForm for UI for .NET MAUI, assign the desired value to the corresponding property in the bound model. The editor reflects the value automatically when the binding is active.

  1. Bind the DataForm to a model using its BindingContext.
  2. Update the property in the model to programmatically set the editor value.
  3. Use the OnEditorGenerated event to customize the editor during its creation.
csharp
public partial class MainPage : ContentPage
{
    GettingStartedModel vm;
    public MainPage()
    {
        InitializeComponent();

        // Initialize the model and bind it to the DataForm
        this.vm = new GettingStartedModel();
        this.dataForm.BindingContext = this.vm;

        // Handle the EditorGenerated event
        this.dataForm.EditorGenerated += this.OnEditorGenerated;
    }

    private void OnEditorGenerated(object sender, DataFormEditorGeneratedEventArgs eventArgs)
    {
        // Customize the editor based on the property name
        switch (eventArgs.PropertyName)
        {
            case "People":
                eventArgs.Editor.HeaderText = "Number of People";
                break;
        }
    }
}

// Model with a property bound to the DataForm editor
public class GettingStartedModel : NotifyPropertyChangedBase
{
    private double? people = 1;

    [Display(Name = "Number of People")]
    public double? People
    {
        get => this.people;
        set => this.UpdateValue(ref this.people, value);
    }
}

See Also