We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to programmatically implement converter to Xamarin.Forms DataForm (SfDataForm)


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");
    }
}

View Sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile