Category / Section
How to programmatically implement converter to Xamarin.Forms DataForm (SfDataForm)
2 mins read
You can apply the converter for Editor in Xamarin.Forms SfDataForm by using custom editor.
You can create and add custom editor to SfDataForm by overriding the DataFormEditor class, where the CustomNumericEditor is inherited using the DataFormEditor<Entry>.
Refer to the online user guide documentation for creating new custom editor in DataForm.
C#
CustomNumericEditor extends DataFormEditor to add converter programmatically.
public class CustomNumericEditor : DataFormEditor<Entry> { public CustomNumericEditor(SfDataForm dataForm) : base(dataForm) { } protected override Entry OnCreateEditorView(DataFormItem dataFormItem) { return new Entry(); } protected override void OnInitializeView(DataFormItem dataFormItem, Entry view) { base.OnInitializeView(dataFormItem, view); view.Keyboard = Keyboard.Numeric; this.OnUpdateValue(dataFormItem, view); this.OnUpdateReadOnly(dataFormItem, view); } protected override void OnWireEvents(Entry view) { view.TextChanged += OnViewTextChanged; view.Focused += View_Focused; view.Unfocused += View_Unfocused; } private void View_Unfocused(object sender, Xamarin.Forms.FocusEventArgs e) { var view = sender as Entry; view.TextColor = Color.Red; if (this.DataForm.CommitMode == Syncfusion.XForms.DataForm.CommitMode.LostFocus || this.DataForm.ValidationMode == ValidationMode.LostFocus) this.OnValidateValue(view); } private void View_Focused(object sender, Xamarin.Forms.FocusEventArgs e) { var view = (sender as Entry); view.TextColor = Color.Green; } private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e) { OnValidateValue(sender as Entry); } protected override bool OnValidateValue(Entry view) { return this.DataForm.Validate("Amount"); } private void OnViewTextChanged(object sender, TextChangedEventArgs e) { var view = sender as Entry; if (DataForm.CommitMode == Syncfusion.XForms.DataForm.CommitMode.PropertyChanged || DataForm.ValidationMode == ValidationMode.PropertyChanged) this.OnValidateValue(view); } protected override void OnCommitValue(Entry view) { var dataFormItemView = view.Parent as DataFormItemView; var value = Convert.ToInt32(view.Text); view.Text = (value * 10).ToString(); this.DataForm.ItemManager.SetValue(dataFormItemView.DataFormItem, view.Text); } protected override void OnUpdateValue(DataFormItem dataFormItem, Entry view) { var cellValue = this.DataForm.ItemManager.GetValue(dataFormItem); if (cellValue != null && view.Text == cellValue.ToString()) return; view.Text = cellValue == null ? string.Empty : cellValue.ToString(); } protected override void OnUpdateReadOnly(DataFormItem dataFormItem, Entry view) { base.OnUpdateReadOnly(dataFormItem, view); } protected override void OnUnWireEvents(Entry view) { view.TextChanged -= OnViewTextChanged; view.Focused -= View_Focused; view.Unfocused -= View_Unfocused; } }
Refer to the following code example for register the editor using RegisterEditor as CustomNumericEditor to make data form item as custom editor in DataForm.
C#
Numeric editor registered to DataForm.
public class DataFormBehavior : Behavior<ContentPage> { SfDataForm dataForm; protected override void OnAttachedTo(ContentPage bindable) { base.OnAttachedTo(bindable); dataForm = bindable.FindByName<SfDataForm>("dataForm"); dataForm.RegisterEditor("Numeric", new CustomNumericEditor(dataForm)); dataForm.RegisterEditor("Amount", "Numeric"); } }