Learn Chained Bind in Blazor
Table of Contents
What is Chained bind in Blazor?
Multiple levels of binding occur at the same time in a “chained bind”. In a chained bind, the event handler and the value must be specified separately i.e. @bind syntax can’t be used to implement chained bind in the page’s element. A parent component however can use the @bind syntax with the child component’s parameter.
Let us open Visual Studio 2019 IDE and load the GetStartedBlazor project that we started in templated components here and then worked through routing components here , lifecycle methods here , and inheritance in Blazor here
In the GetStartedBlazor project, within the Pages folder, create a razor file AccountField.razor. Now paste the following code in this file.
Create a Component: AccountField.razor
In the AccountField component, the input text field on changing (inputting some text) shows either the text entered or password field (all dots) depending on the value of a boolean field that toggles between true (or false) when a button is clicked. The account is validated against inputting empty spaces in the input text. If the account (input text) is changed an event is raised and notified to the parent component that uses the AccountField component.
If we put a space within the account no. it immediately validates it with the given message.
Account:
<input @oninput="OnAccountChanged" required
type="@(showAccount ? "text" : "password")"
value="@Account" />
<button class="btn btn-primary" @onclick="ToggleShowAccount">Show Account</button>
<span class="text-danger"> @validationMessage</span>
@code
{
private bool showAccount;
private string account;
private string validationMessage;
[Parameter]
public string Account
{
get { return account ?? string.Empty; }
set
{
if(account != value)
{
if(value.Contains(' '))
{
validationMessage = "No spaces allowed";
}
else
{
account = value;
validationMessage = string.Empty;
}
}
}
}
[Parameter]
public EventCallback<string> AccountChanged { get; set; }
private Task OnAccountChanged(ChangeEventArgs e)
{
Account = e.Value.ToString();
return AccountChanged.InvokeAsync(Account);
}
private void ToggleShowAccount()
{
showAccount = !showAccount;
}
}
Create a Parent Component AccountSetter.razor (that consumes the AccountField)
Now we can use the AccountField component by creating an AccountSetter component that is data-bound to the Account property of the child (AccountField) component as follows
@page "/AccountSet"
<AccountField @bind-Account ="account"></AccountField>
@code {
private string account;
}
In the code above for AccountSetter, @bind-Account is a chained bind to the Account property in the AccountField component which is assigned to the local string field named account.
Run the application
Run the application with CTRL+F5 and browse as below, we shall see the following screen.

If we now click on the button ShowAccount we get the password fields

On putting whitespace in the account box we get the validation message:

On removing the spaces above in the account field, the validation message is cleared

YouTube Version
The YouTube version of this tutorial is here
Follow Me Here