Giter Site home page Giter Site logo

syncfusionexamples / programatic-converter-dataform-xamarin Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 577 KB

This repository contains a sample on How to programmatically implement converter in the Syncfusion Xamarin.Forms Dataform (SfDataform)?

C# 100.00%
dataform sfdataform xamarin xamarin-forms converter

programatic-converter-dataform-xamarin's Introduction

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.

Refer to the online user guide documentation for creating new custom editor in DataForm.

You can also refer the following article.

https://www.syncfusion.com/kb/11541/how-to-programmatically-implement-converter-to-xamarin-forms-dataform-sfdataform

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

programatic-converter-dataform-xamarin's People

Contributors

jayaleshwari avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 avatar

Forkers

vishalomprasad

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.