TextBox value binding to object type

Hello @Team.

I'm trying to bind object properties to some components. It worked normally with the CheckBox and with the Numeric, however the TextBox throws a compilation error.

After checking the TextBox and the Numeric implementations, it seems that it only needs a little tweak with generics to make this work.

Hi @kim,

Not sure I understand the problem. What is the compilation error and what’s the generics tweak?

Let's say I have a List of WidgetSetting containing values of different types, like bool, int and string:

public class WidgetSetting {
    public string name { get; set; }
    public string property { get; set; }
    public object value { get; set; }
    public Type type { get; set; }
}

Below there are two settings examples:

new WidgetSetting() {
    name = Front.Language.Service.GetSetting(EN_Setting.Widget_AccessList_Count),
    property = nameof(count),
    value = 100,
    type = typeof(int)
},
new WidgetSetting() {
    name = Front.Language.Service.GetSetting(EN_Setting.Widget_AccessList_Pagination),
    property = nameof(pagination),
    value = true,
    type = typeof(bool)
}

When binding the value to a CheckBox and to a Numeric, it works as expected:



However the TextBox throws the following compilation error:

This is because the binded value is expected to be a string:

@inherits FormComponent<string>

In Numeric and CheckBox a generic value is expected:

@typeparam TValue
@inherits FormComponent<TValue>

Which later on is manipulated like so:

Value = (TValue)ConvertType.ChangeType(newValue, typeof(TValue));
...
TValue newValue;
BindConverter.TryConvertTo<TValue>($"{args.Value}", System.Globalization.CultureInfo.CurrentCulture, out newValue);
...
await JSRuntime.InvokeAsync<string>("Radzen.setInputValue", input, Value);

It seems that this can also work in the TextBox component, and probably in every other component.

Hi @kim,

The TextBox can work only with strings while CheckBox TValue can be bool or bool?. The Numeric component can accept any numeric type. This is the reason behind TValue object for both CheckBox and Numeric.

Alright, managed to get this done by adding another variable:

public class WidgetSetting {
    public string name { get; set; }
    public string property { get; set; }
    public object value { get; set; }
    public Type type { get; set; }
    public string valueString { get { return type == typeof(string) ? (string)value : "not string"; } set { this.value = value; } }
}

And then binding it to the TextBox:

Even though this fixed my problem, the results are quite interesting so I'll share them with you. I created this setting of type string:

new WidgetSetting() {
    name = Front.Language.Service.GetSetting(EN_Setting.Widget_AccessList_RefreshRate),
    property = nameof(test),
    value = "testing type string",
    type = typeof(string)
}

And the numeric surprisingly was capable of storing the string:

Until we have typeparam constrains the best option will stay object.

1 Like

Just a little follow up on a new bug regarding this context. The checkbox works perfectly:

The numeric, however, works only if using the up and down buttons:

If I manually write a number, it will throw the following error:

As I said in my previous reply there is not much we can do until we have type constrains. The numeric should work normally if you specify numeric type as TValue or simply bind it to a numeric type property. Binding to object is not supported.