Here, you can see, how to realize two-way data-binding between parent and child component. The example works well for int
s and there is also an answer for double
s. But there seems to be a problem with the culture info. Also, the behavior is different in different browsers. The following code works well for enet, who answered above linked question:
Index.razor
@page "/"
<Child @bind-Value="@Value" Min="0" Max="100" />
<div>@Value.ToString()</div>
@code
{
private double Value { get; set; } = 10.1; // default value
}
Child.razor
<div>
<input type="number" value="@Value"
@onchange="@((args) => ValueChanged.InvokeAsync(Convert.ToDouble(args.Value)) )"
min="@Min" max="@Max" step="0.01">
</div>
@code {
[Parameter] public double Value { get; set; }
[Parameter] public EventCallback<double> ValueChanged { get; set; }
[Parameter] public double Max { get; set; }
[Parameter] public double Min { get; set; }
}
However, it does not work for me, probably because of the CultureInfo
, which is German in my case. Edge and Chrome show empty input
s on the GUI. Firefox shows "10,1" (with a comma as decimal separator). Clicking the upper spin button, changes the value to "10.11" (with a dot) for a second, what is then automatically set to "1011". The following images show the state before click, during click and after click:
I changed
@onchange="@((args) => ValueChanged.InvokeAsync(Convert.ToDouble(args.Value)) )"
to
@onchange="@((args) => ValueChanged.InvokeAsync(Convert.ToDouble(args.Value, System.Globalization.CultureInfo.InvariantCulture)) )"
This does not change Edge's behavior but Firefox results in my expected value:
Satisfied for the moment, I added the parameter [Parameter] public double Step { get; set; }
and step="@Step"
in Child.razor as well as Step="0.01"
in Index.razor. I expected the same behavior but now after click it is:
Questions:
- How can I make this code really culture-invariant?
- Why is there a different behavior with the parameter
Step
?
Edit
I have the prove now, that it is a CultureInfo
issue. To reproduce, please do the following steps (tested with Edge):
- If not already, change Edge's language to English.
- Go to the BlazorFiddle, I created. You should see "10.1" in the input and also "10.1" in the
div
. - Change Edge's language to German.
- Restart Edge and visit the Fiddle again. You should see an empty input and "10,1" in the
div
now.
This only happens, when I use the input in a child component, but not when it is used in the parent.