RadDataForm with expandoObject

4 posts, 0 answers
  1. Vivien
    Vivien avatar
    6 posts
    Member since:
    Apr 2016

    Posted 06 Jul Link to this post

    Hello,

    I want to display a DataForm for enter a new element in a grid. But this grid is generated dynamically.

    For that, i use a: "dynamic data = new ExpandoObject()" and feed that with: "data.field1 = "string"; data.field2 = 10" and bind my DataForm on it: "<telerik:RadDataForm x:Name="RadDataForm" AutoGenerateFields="True"
                                         AutoCommit="True" EditEnded="DataForm_OnEditEnded"
                                         AutoEdit="True" CommandButtonsVisibility="Commit,Cancel"
                                         CurrentItem="{Binding Path=DataContext.data, RelativeSource={RelativeSource Self}}"
                                         Header="Hello world"
                                         KeyboardNavigation.TabNavigation="Cycle">
    </telerik:RadDataForm>"

    My problem now, the DataForm change my Int type on string type...

    Have you some idea?

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 11 Jul Link to this post

    Hello Vivien,

    The reason you're observing this behavior is because the ExpandoObject's values are of type object. You could, however, handle RadDataForm's AutoGeneratingField event and process the values there. Here's an example, following the approach described in this article:

    private void RadDataForm_AutoGeneratingField(object sender, Telerik.Windows.Controls.Data.DataForm.AutoGeneratingFieldEventArgs e)
    {
        var property = (RadDataForm.CurrentItem as IDictionary<string, object>)[e.PropertyName];
        if (IsNumericType(property.GetType()))
        {
            var binding = e.DataField.DataMemberBinding;
            binding.Converter = new IntToNullableDoubleConverter();
            e.DataField = new DataFormNumericUpDownField() { Label = e.DataField.Label, DataMemberBinding = binding };
        }
    }
     
    public static bool IsNumericType(Type type)
    {
        TypeCode typeCode = Type.GetTypeCode(type);
        //The TypeCode of numerical types are between SByte (5) and Decimal (15).
        return (int)typeCode >= 5 && (int)typeCode <= 15;
    }

    I hope you find this helpful.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Vivien
    Vivien avatar
    6 posts
    Member since:
    Apr 2016

    Posted 13 Jul in reply to Dilyan Traykov Link to this post

    Thanks you, I try that, it fell good :)
  5. Vivien
    Vivien avatar
    6 posts
    Member since:
    Apr 2016

    Posted 28 Jul in reply to Dilyan Traykov Link to this post

    Thanks :)

     

Back to Top