Articles in this section
Category / Section

How to recognize the touch gesture for SfDataForm label and editor when data form is read only ?

1 min read

 

If SfDataForm is read only, you cannot interact with the data form label and editor. To recognize the touch gesture of label and editor, add the gesture recognizer by initializing the UITapGestureRecognizer class for DataFormItemView.

 

 

 
Employees.cs
 
   public class Employees
    {
        public int EmployeeID { get; set; }
        public string Title { get; set; }
        [DisplayOptions(ImageSource = "ContactID.png")]
        public decimal ContactID { get; set; }
    }

 

Refer to the following code example for binding DataObject, setting data form as read only, and enabling user interaction.

 

 
            dataForm = new SfDataForm(new CoreGraphics.CGRect(30, 0, this.View.Bounds.Width, this.View.Bounds.Height))
            {
                DataObject =
                new Employees()
                {
                    ContactID = 1001,
                    EmployeeID = 1709,
                    Title = "Software"
                },
                BackgroundColor = UIColor.White
            };
 
            dataForm.IsReadOnly = true;
            dataForm.UserInteractionEnabled = true;
            this.View = dataForm;
 

 

By using the CreateDataFormItemView override method of DataFormLayoutManager class, you can add the gesture recognizer by initializing UITapGestureRecognizer for DataFormItemView that recognizes the gesture. You can also find the touched data form editor or label based on the touch position in frame, even data form has read only access.

 

Refer to the following code example for initializing UITapGestureRecognizer and getting touched data form label or editor using touch position.

 

 
  dataForm.LayoutManager = new DataFormLayoutManagerExt(dataForm);
 
   public class DataFormLayoutManagerExt : DataFormLayoutManager
    {
        public DataFormLayoutManagerExt(SfDataForm dataForm) : base(dataForm)
        {
 
        }
 
        protected override DataFormItemView CreateDataFormItemView(int rowIndex, int columnIndex)
        {
            var view = base.CreateDataFormItemView(rowIndex, columnIndex);
            view.UserInteractionEnabled = true;
            // Gesture added for DataformItemView
            UITapGestureRecognizer gestureRecognizers = new UITapGestureRecognizer();
            gestureRecognizers.NumberOfTapsRequired = 1;
            if (view != null)
            {                
                view.UserInteractionEnabled = true;
                gestureRecognizers.AddTarget(() =>
                {
                    // event raise for DataFormItemView
                    OnClickEvent(gestureRecognizers);
                });
                view.AddGestureRecognizer(gestureRecognizers);
            }
            return view;
        }
 
        private void OnClickEvent(UITapGestureRecognizer gestureRecognizer)
        {
            var dataFormItemView = gestureRecognizer.View;
            var touchPosition = gestureRecognizer.LocationInView(dataFormItemView);
 
 
            var labelFrame = ((dataFormItemView as DataFormItemView).Subviews[0] as UIView).Frame;
 
            var editorFrame = ((dataFormItemView as DataFormItemView).EditorView as UIView).Frame;
 
            // Editor element has been tapped whether the below condition is true
            if (editorFrame.Contains(touchPosition.X, touchPosition.Y))
            {
                // your code here
            }
            // Label/Image has been tapped whether the below condition is true.
            else if (labelFrame.Contains(touchPosition.X, touchPosition.Y))
            {
                // your code here
            }
        }
    }
 

 

Click here to download the sample.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied