You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Parent-to-child binding with component parameters
128
+
## Binding with component parameters
129
+
130
+
A common scenario is binding a property in a child component to a property in its parent. This scenario is called a *chained bind* because multiple levels of binding occur simultaneously.
129
131
130
132
Component parameters permit binding properties and fields of a parent component with `@bind-{PROPERTY OR FIELD}` syntax.
131
133
134
+
Chained binds can't be implemented with [`@bind`](xref:mvc/views/razor#bind) syntax in the child component. An event handler and value must be specified separately to support updating the property in the parent from the child component.
135
+
136
+
The parent component still leverages the [`@bind`](xref:mvc/views/razor#bind) syntax to set up the data-binding with the child component.
137
+
132
138
The following `Child` component (`Shared/Child.razor`) has a `Year` component parameter and `YearChanged` callback:
133
139
134
140
```razor
@@ -139,16 +145,25 @@ The following `Child` component (`Shared/Child.razor`) has a `Year` component pa
139
145
</div>
140
146
</div>
141
147
148
+
<button @onclick="UpdateYearFromChild">Update Year from Child</button>
149
+
142
150
@code {
151
+
private Random r = new Random();
152
+
143
153
[Parameter]
144
154
public int Year { get; set; }
145
155
146
156
[Parameter]
147
157
public EventCallback<int> YearChanged { get; set; }
The callback (<xref:Microsoft.AspNetCore.Components.EventCallback%601>) must be named as the component parameter name followed by the "`Changed`" suffix (`{PARAMETER NAME}Changed`). In the preceding example, the callback is named `YearChanged`. For more information on <xref:Microsoft.AspNetCore.Components.EventCallback%601>, see <xref:blazor/components/event-handling#eventcallback>.
166
+
The callback (<xref:Microsoft.AspNetCore.Components.EventCallback%601>) must be named as the component parameter name followed by the "`Changed`" suffix (`{PARAMETER NAME}Changed`). In the preceding example, the callback is named `YearChanged`. <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType> invokes the delegate associated with the binding with the provided argument and dispatches an event notification for the changed property.
152
167
153
168
In the following `Parent` component (`Parent.razor`), the `year` field is bound to the `Year` parameter of the child component:
154
169
@@ -182,13 +197,7 @@ By convention, a property can be bound to a corresponding event handler by inclu
A common scenario is chaining a data-bound parameter to a page element in the component's output. This scenario is called a *chained bind* because multiple levels of binding occur simultaneously.
188
-
189
-
A chained bind can't be implemented with [`@bind`](xref:mvc/views/razor#bind) syntax in the child component. The event handler and value must be specified separately. A parent component, however, can use [`@bind`](xref:mvc/views/razor#bind) syntax with the child component's parameter.
190
-
191
-
The following `PasswordField` component (`PasswordField.razor`):
200
+
In a more sophisticated and real-world example, the following `PasswordField` component (`PasswordField.razor`):
192
201
193
202
* Sets an `<input>` element's value to a `password` field.
194
203
* Exposes changes of a `Password` property to a parent component with an [`EventCallback`](xref:blazor/components/event-handling#eventcallback) that passes in the current value of the child's `password` field as its argument.
@@ -300,6 +309,8 @@ Password:
300
309
}
301
310
```
302
311
312
+
For more information on <xref:Microsoft.AspNetCore.Components.EventCallback%601>, see <xref:blazor/components/event-handling#eventcallback>.
313
+
303
314
## Bind across more than two components
304
315
305
316
You can bind through any number of nested components, but you must respect the one-way flow of data:
0 commit comments