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
close icon

Defining a DataFormPickerItem using XAML does not work

I have a class containing the following property 

        [EnumDataType(typeof(Gender))]
        public Gender Gender
        {
            get { return _gender; }
            set { SetProperty(ref _gender, value); }
        }

Gender is a plain enum as shown below:

public enum Gender
    {
        Male,
        Female
    }

On the SfDataForm control, when I use AutoGenerateItems="True" everything is fine and I get a Picker to select a gender.

If I try to define the SfDataForm control using XAML such as in the following example, it doesn't work.

        <dataForm:SfDataForm x:Name="dataform" Grid.Row="0" DataObject="{Binding Athlete}" AutoGenerateItems="False">
            <dataForm:SfDataForm.Items>
                <dataForm:DataFormTextItem Name="FirstName" LabelText="{x:Static res:Strings.AthleteFirstNameFieldName}" PlaceHolderText="{x:Static res:Strings.AthleteFirstNameFieldPlaceHolderText}" Editor="Text"/>
                <dataForm:DataFormTextItem Name="Initials" LabelText="{x:Static res:Strings.AthleteInitialsFieldName}" Editor="Text"/>
                <dataForm:DataFormTextItem Name="LastName" LabelText="{x:Static res:Strings.AthleteLastNameFieldName}" PlaceHolderText="{x:Static res:Strings.AthleteLastNameFieldPlaceHolderText}" Editor="Text"/>
                <dataForm:DataFormPickerItem Name="Gender" LabelText="{x:Static res:Strings.AthleteGenderFieldName}" Editor="Picker"/>
                <dataForm:DataFormDateItem Name="BirthDate" LabelText="{x:Static res:Strings.AthleteBirthDateFieldName}" Editor="Date" MaximumDate="{x:Static sys:DateTime.Now}"/>
                <dataForm:DataFormTextItem Name="Email" LabelText="{x:Static res:Strings.AthleteEmailFieldName}" Editor="Text" KeyBoard="Keyboard.Email"/>
                <dataForm:DataFormTextItem Name="Notes" LabelText="{x:Static res:Strings.AthleteNotesFieldName}" Editor="MultilineText"/>
            </dataForm:SfDataForm.Items>
        </dataForm:SfDataForm>

Any idea?
Do you have an example?


6 Replies

SP Subburaj Pandian Veluchamy Syncfusion Team May 24, 2019 01:17 PM UTC

Hi Jean, 
  
Thank you contacting Syncfusion support. 
  
We have analyzed your query “DataFormPickerItem using XAML doesn’t work with enum value”. As per DataForm implementation, if you need to create new DataFormPickerItem in XAML you need to provide ItemsSource value. Since DataForm creates new DataFormItems from XAML it doesn’t consider property type.  
 
We have prepared sample for the same, 
  
Sample link: DataForm 
  
We hope that this helps. Kindly revert us if you have any concern. 
 
Regards,
Subburaj Pandian V     



JE Jean-Marc May 24, 2019 03:48 PM UTC

From you example, I see that I have to supply explicitly a collection of values.
Instead of creating a collection in the ViewModel as you did, I created a converter to do this.

So now my XAML becomes:

                    Name="Gender"
                    Editor="Picker"
                    ItemsSource="{Binding Path=Gender, Converter={conv:EnumToCollectionConverter}, Mode=OneTime}"
                    LabelText="{x:Static res:Strings.AthleteGenderFieldName}" />

The converter is as follow:

public class EnumToCollectionConverter : IValueConverter, IMarkupExtension
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Enum.GetValues(value.GetType());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

When I run this, I get the following exception:
Unhandled Exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: source occurred



SP Subburaj Pandian Veluchamy Syncfusion Team May 27, 2019 01:18 PM UTC

Hi Jean, 
  
Thank you for the update. 
  
Currently, we are analyzing the reported issue “ItemsSource for DataFormPickerItem doesn’t take enum collection with converter while creating through XAML”. We will validate and update you further details in two business days (May 29, 2019). We appreciate your patience until then. 
 
Regards,
Subburaj Pandian V       



SP Subburaj Pandian Veluchamy Syncfusion Team May 29, 2019 01:12 PM UTC

Hi Jean, 
  
Thank you for your patience. 
  
We have analyzed the reported issue “ItemsSource for DataFormPickerItem doesn’t take enum collection with converter while creating item through XAML”. As per DataForm implementation ItemsSource is IEnumerable type which will accept collection values. If you want to populate it with Enum collection, you can achieve it by converting Enum list as ObservableCollection and bind that collection to ItemsSource. 
  
Sample link:  DataForm 
  
We hope that this helps. Kindly revert us if you have any concern. 
  
Regards,
Subburaj Pandian V  



JE Jean-Marc May 29, 2019 03:03 PM UTC

Hi,

I did try to have my converter return an ObservableCollection<string> as shown below:

    public class EnumToCollectionConverter : IValueConverter, IMarkupExtension
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new ObservableCollection<string>(Enum.GetNames(typeof(Gender)));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

XAML fragment for the DataForm is as follow:

                <dataForm:DataFormPickerItem
                    Name="Gender"
                    BindingContext="{Binding Source={x:Reference viewBindingContext}, Path=BindingContext}"
                    Editor="Picker"
                    ItemsSource="{Binding Path=Gender, Converter={conv:EnumToCollectionConverter}}"
                    LabelText="{x:Static res:Strings.AthleteGenderFieldName}" />

However this does not work as it seems that the EnumToCollectionConverter never gets called.
Can you supply an example with a converter instead of just binding directly to an ObservableCollection?

Thanks again,
JMD


SP Subburaj Pandian Veluchamy Syncfusion Team May 30, 2019 10:33 AM UTC

Hi Jean, 
  
Thank you for contacting Syncfusion support. 
  
We have analyzed your query “ItemSource for DataFormPickerItem doesn’t take enum collection with converter while creating item through XAML”, from the code snippet you have shared we suspect that you need to specify property which is type of enum as binding path, you have specified enum directly hence converter doesn’t call. We have attached a simple sample for the same. 
  
Sample link: DataForm 
  
We hope that this helps. Kindly revert us if you have any concern. 
 
Regards,
Subburaj Pandian V  


Loader.
Live Chat Icon For mobile
Up arrow icon