WPF-Forge/Forge.Forms

Dynamic form setting initial check boxes

hashitha opened this issue · 4 comments

There is an issue where the UpdateDefaultValue doesn't update the checkboxes. This was implemented in #18

e.g. if you try to load the following json object by using

{
  "FirstName": "john",
  "LastName": "doe",
  "DateOfBirth": "2018-10-08T00:00:00",
  "Username": null,
  "Password": null,
  "PasswordConfirm": null,
  "Agree": true
}

by using a code like this

JObject o = JObject.Parse(json);

                    foreach (var item in o.Children())
                    {
                        if (item is JProperty)
                        {
                            var name = ((JProperty)item).Name;
                            var value = ((JProperty)item).Value;
                            CompiledDefinition.UpdateDefaultValue(name, value);
                        }
                    }

it updates all fields except the checkboxes

image

I see the problem, I'm looking into it.

What's weird is that the value is being written... if I serialize the dynamo to JSON after resetting I get a true value... I think this has something to do with the checkbox binding

Ah, found it!

var o = JObject.Parse(json);
foreach (var property in o)
{
    var name = property.Key;
    var value = property.Value;
    CompiledDefinition.UpdateDefaultValue(name, value);
}

property.Value is not a literal value (bool, string, etc). It is actually a JToken, which happens to be a JValue

This is how you can fix it:

var o = JObject.Parse(json);
foreach (var property in o)
{
    var name = property.Key;
    object value;
    if (property.Value is JValue v)
    {
        value = v.Value; // We unpack our bool (or whatever value) here...
    }
    else
    {
        // What to do if the child is a JObject or JArray?
        value = null;
    }

    CompiledDefinition.UpdateDefaultValue(name, value);
}

Otherwise you are writing the JValue to the expando. The strings working is just a coincidence because it was calling .ToString() for the object.

@edongashi this is great! it is working now. Thanks for your help!