How do you implement two-way binding for a custom component parameter using the Value/ValueChanged pattern?
Last updated
Expose a
Valueparameter plus anEventCallback<T>namedValueChangedso the parent can use@bind-Value(any name works:Foo+FooChanged).The parent passes the current value in, and the child calls
ValueChanged.InvokeAsync(newValue)to update the parent's field and re-render it.Optionally add
ValueExpressionfor form validation integration.
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-10.0
- https://www.ssw.com.au/rules/blazor-use-bind-value-after-instead-of-valuechanged
- https://www.reddit.com/r/Blazor/comments/17pgslx/how_to_create_blazor_component_that_uses_twoway/
- https://www.telerik.com/blazor-ui/documentation/knowledge-base/value-changed-validation-model
- https://docs.devexpress.com/Blazor/403279/troubleshooting/common-component-issues/the-component-parameter-value-changed-is-used-two-or-more-times
- https://www.meziantou.net/two-way-binding-in-blazor.htm
- https://blazor-university.com/components/two-way-binding/