|
| 1 | +--- |
| 2 | +title: Setting Editor Value Programmatically in DataForm for UI for .NET MAUI |
| 3 | +description: Learn how to programmatically set the value of a specific editor in the DataForm for UI for .NET MAUI. |
| 4 | +type: how-to |
| 5 | +page_title: Programmatically Set Editor Value in UI for .NET MAUI DataForm |
| 6 | +meta_title: Programmatically Set Editor Value in UI for .NET MAUI DataForm |
| 7 | +slug: programmatically-set-editor-value-dataform-maui |
| 8 | +tags: dataform, editorgenerated, set-value, ui-for-dotnet-maui |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.0.1 | DataForm for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How can I set a value to a DataForm editor programmatically? |
| 24 | +- How to use the `EditorGenerated` event in DataForm for UI for .NET MAUI? |
| 25 | +- How to bind model properties to DataForm editors? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +1. Bind the DataForm to a model using its `BindingContext`. |
| 32 | +2. Update the property in the model to programmatically set the editor value. |
| 33 | +3. Use the `OnEditorGenerated` event to customize the editor during its creation. |
| 34 | + |
| 35 | +```csharp |
| 36 | +public partial class MainPage : ContentPage |
| 37 | +{ |
| 38 | + GettingStartedModel vm; |
| 39 | + public MainPage() |
| 40 | + { |
| 41 | + InitializeComponent(); |
| 42 | + |
| 43 | + // Initialize the model and bind it to the DataForm |
| 44 | + this.vm = new GettingStartedModel(); |
| 45 | + this.dataForm.BindingContext = this.vm; |
| 46 | + |
| 47 | + // Handle the EditorGenerated event |
| 48 | + this.dataForm.EditorGenerated += this.OnEditorGenerated; |
| 49 | + } |
| 50 | + |
| 51 | + private void OnEditorGenerated(object sender, DataFormEditorGeneratedEventArgs eventArgs) |
| 52 | + { |
| 53 | + // Customize the editor based on the property name |
| 54 | + switch (eventArgs.PropertyName) |
| 55 | + { |
| 56 | + case "People": |
| 57 | + eventArgs.Editor.HeaderText = "Number of People"; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Model with a property bound to the DataForm editor |
| 64 | +public class GettingStartedModel : NotifyPropertyChangedBase |
| 65 | +{ |
| 66 | + private double? people = 1; |
| 67 | + |
| 68 | + [Display(Name = "Number of People")] |
| 69 | + public double? People |
| 70 | + { |
| 71 | + get => this.people; |
| 72 | + set => this.UpdateValue(ref this.people, value); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## See Also |
| 78 | + |
| 79 | +- [UI for .NET MAUI DataForm Overview](https://docs.telerik.com/devtools/maui/controls/dataform/overview) |
| 80 | +- [UI for .NET MAUI DataForm Editor Properties](https://docs.telerik.com/devtools/maui/controls/dataform/editors/overview#common-properties-for-built-in-editors) |
0 commit comments