Programmatically access picker registered in a dataform

Could you show how to modify the picker mode for a picker that is created using the dataform.registerEditor("ClassPropertyID", "Picker") method?

I'm setting the displaymemberPath and ValueMember path in the  DataForm_OnAuthGeeneratingDataFormItem event in my codebehind, but I'm not able to programmatically get the current dataItem's editortype so I can set the PickerMode to Dialog.

'dataForm.SourceProvider = new CustomSourceProvider();
dataForm.RegisterEditor("SchoolID", "Picker");

void DataForm_OnAutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
{
if (e.DataFormItem == null)
return;
  
if (e.DataFormItem.Name == "SchoolID" )
{
(e.DataFormItem as DataFormPickerItem).DisplayMemberPath = "Name";
(e.DataFormItem as DataFormPickerItem).ValueMemberPath = "ID";
//The line below is what I want to do, but this is incorrect because the sender is a dataForm
//(sender as SfPicker).PickerMode = PickerMode.Dialog;
}     
}'

3 Replies

JN Jayaleshwari N Syncfusion Team September 6, 2018 12:32 PM UTC

Hi Lyndon,  
  
Thank you for contacting Syncfusion support.  
  
We have checked with your query to modify the picker mode for a picker using SfPicker and your requirement can be achieved by adding custom editor by using DataFormEditor class which is used to add own custom editor in SfDataForm. In Xamarin.Forms Picker, PickerMode property is not available. Hence in the custom renderer using SfPicker your requirement can be achieved.  
   
Please refer below online user guide documentation for creating new custom editor in SfDataForm.  
   
Custom Editor:  
In the sample, we have passed a View as custom control and in the native renderer have set EditText as NativeControl and in the touch of the edit text, picker is made open. Picker mode is set as Dialog as mentioned by you.  
  
Code snippet:  
  
For Android:  
[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerEditor))]  
namespace DataFormPicker.Droid  
 
    public class CustomPickerEditor : ViewRenderer  
     
  
        public CustomPickerEditor()  
         
  
         
          
        EditText editText;  
        protected override voidOnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)  
         
            base.OnElementChanged(e);  
  
            if (e.NewElement != null 
             
                editText = new EditText(this.Context);  
                (App.Current.MainPage as MainPage).Picker.SelectionChanged += Picker_SelectionChanged;  
                editText.Touch += EditText_Touch;  
                editText.Text = ((App.Current.MainPage asMainPage).Picker.ItemsSource).Cast<object>().FirstOrDefault().ToString();  
                editText.SetBackgroundColor(Android.Graphics.Color.LightGray);  
                this.SetNativeControl(editText);  
             
         
        private void Picker_SelectionChanged(object sender, Syncfusion.SfPicker.XForms.SelectionChangedEventArgs e)  
         
            editText.Text = (App.Current.MainPage asMainPage).Picker.SelectedItem.ToString();  
         
  
        private void EditText_Touch(object sender, TouchEventArgs e)  
         
            (App.Current.MainPage as MainPage).Picker.IsOpen = true 
         
     
 
  
  
For iOS:  
  
  
[assembly: ExportRenderer(typeof(CustomPicker), typeof(CustomPickerEditor))]  
namespace DataFormPicker.iOS  
 
    public class CustomPickerEditor : ViewRenderer  
     
        public CustomPickerEditor()  
         
             
         
        UIButton editText;  
  
        protected override void OnElementChanged(ElementChangedEventArgs<View> e)  
         
            base.OnElementChanged(e);  
            if(e.NewElement != null 
             
                editText = new UIButton();    
                (App.Current.MainPage asMainPage).Picker.SelectionChanged+=Picker_SelectionChanged;;  
                editText.SetTitle (((App.Current.MainPage asMainPage).Picker.ItemsSource).Cast<object>().FirstOrDefault().ToString(),UIControlState.Normal);  
                editText.BackgroundColor = UIColor.Gray;  
                editText.TouchUpInside += EditText_TouchUpInside;  
                this.SetNativeControl(editText);  
            }            
         
  
        private void Picker_SelectionChanged(object sender, Syncfusion.SfPicker.XForms.SelectionChangedEventArgs e)  
         
            editText.SetTitle((App.Current.MainPage asMainPage).Picker.SelectedItem.ToString(), UIControlState.Normal);  
         
  
        private void EditText_TouchUpInside(object sender, EventArgs e)  
         
            (App.Current.MainPage as MainPage).Picker.IsOpen = true 
         
     
 
  
  
Sample:  DataFormPicker
 
 
  
Screenshots: screenshots
  
  
Note   
In order to update the DataObject values when using custom editor, you should update and validate the  DataForm Item editor value manually by using OnCommitValue and OnValidateValue override method of DataFormEditor class on the respective event which is used to commit and validate the customized value in DataObject based on your requirement.      
  
Please let us know whether provided solution meets your requirement  
 
Regards, 
Jayaleshwari N 



LH Lyndon Hughey September 6, 2018 07:25 PM UTC

Thanks for providing this sample.  While it does run, it presents some architectural & maintenance issues. I would like for every instance of the SfPicker in the app to open via a dialog, so it seems like one should be able to override the property on a global scale or have some way to bait and switch the picker for a SFPicker in the platform specific code.

Another way would be to have a SfPicker be created rather than a XF picker.  

Once again, I appreciate the sample and I will probably decide to abandon the DataForm if there isn't a more elegant way.


JN Jayaleshwari N Syncfusion Team September 7, 2018 07:19 AM UTC

Hi Lyndon,  
  
We are checking your requirement for Dialog PickerMode in Picker. Currently this feature is not available in our Picker in SfDataForm. We have used alert dialog for Picker in android and UIPickerView for iOS. If you could confirm some specific details regarding the requirement, it would help us to analyze and check whether to implement the feature in future in line with your requirement. 

If our definition of this feature is different from your requirement then let us know the specific details (if Screenshot possible), so that we can work towards fulfilling your requirements.
 
 
Regards, 
Jayaleshwari N 


Loader.
Up arrow icon