英文:
Why is EditContext.OnFieldChanged not firing?
问题
我已经按如下方式设置了我的代码:
protected override async Task OnInitializedAsync()
{
Value ??= new AddressFormPageModel();
Context = new EditContext(Value);
Context.OnFieldChanged += OnFieldChanged;
}
private async void OnFieldChanged(object? sender, FieldChangedEventArgs e)
{
IsValidated = false;
if (ValueChanged.HasDelegate)
await ValueChanged.InvokeAsync(Value);
}
而在我的Razor文件中,我有以下内容:
<DxFormLayout>
<DxFormLayoutItem Caption="Street Address:" Field="@nameof(Value.StreetNumberAndName)" ColSpanMd="12">
<DxTextBox @bind-Text="@Value!.StreetNumberAndName"
ReadOnly="@ReadOnly"
ShowValidationIcon="true" />
</DxFormLayoutItem>
...
当我编辑“Street Address:”并退出时,OnFieldChanged
没有被调用。当我调用 Submit
时,Value对象被正确地填充了 StreetNumberAndName
的值。
我在Value.StreetNumberAndName的setter上设置了断点,当我从编辑框切换到下一个时,它被设置。
有没有任何想法为什么 OnFieldChanged
没有被调用?
更新:
这是一个内部组件。它在以下位置:
<EditForm EditContext="@EditContext"
OnValidSubmit="@HandleValidSubmitAsync"
OnInvalidSubmit="@HandleInvalidSubmitAsync"
Context="@EditFormContext">
<ObjectGraphDataAnnotationsValidator />
<CustomValidation @ref="_customValidation" />
<DxFormLayout>
<DxFormLayoutItem Caption="Unique Id:" Field="@nameof(Model.UniqueId)" ColSpanMd="4">
<DxTextBox @bind-Text="@Model.UniqueId"
ReadOnly="@(ModeState != Mode.Create)"
ShowValidationIcon="true" />
</DxFormLayoutItem>
<AddressForm @ref="_addressElement"
ReadOnly="@(ModeState == Mode.Read)"
OnValidated="@OnAddressValidated"
ValueChanged="@OnAddressChanged"
Value="@Model.AddressModel">
</AddressForm>
</DxFormLayout>
...
希望这可以帮助您解决 OnFieldChanged
没有被调用的问题。
英文:
I have set up my code as follows:
protected override async Task OnInitializedAsync()
{
Value ??= new AddressFormPageModel();
Context = new EditContext(Value);
Context.OnFieldChanged += OnFieldChanged;
}
private async void OnFieldChanged(object? sender, FieldChangedEventArgs e)
{
IsValidated = false;
if (ValueChanged.HasDelegate)
await ValueChanged.InvokeAsync(Value);
}
And in my razor file I have:
<DxFormLayout >
<DxFormLayoutItem Caption="Street Address:" Field="@nameof(Value.StreetNumberAndName)" ColSpanMd="12">
<DxTextBox @bind-Text="@Value!.StreetNumberAndName"
ReadOnly = "@ReadOnly"
ShowValidationIcon="true" />
</DxFormLayoutItem>
...
When I edit the "Street Address:" and exit it, the OnFieldChanged
is not called. When I call Submit
the Value object is correctly populated with the StreetNumberAndName
value.
I did set a breakpoint on the Value.StreetNumberAndName setter and it is set when I tab from that edit box to the next.
Any idea why OnFieldChanged
is not being called?
Update:
This is an inner component. It is <AddressForm>
in the following:
<EditForm EditContext="EditContext"
OnValidSubmit="HandleValidSubmitAsync"
OnInvalidSubmit="HandleInvalidSubmitAsync"
Context="EditFormContext">
<ObjectGraphDataAnnotationsValidator />
<CustomValidation @ref="_customValidation" />
<DxFormLayout>
<DxFormLayoutItem Caption="Unique Id:" Field="@nameof(Model.UniqueId)" ColSpanMd="4">
<DxTextBox @bind-Text="@Model.UniqueId"
ReadOnly="@(ModeState != Mode.Create)"
ShowValidationIcon="true" />
</DxFormLayoutItem>
<AddressForm @ref="_addressElement"
ReadOnly="@(ModeState == Mode.Read)"
OnValidated="OnAddressValidated"
ValueChanged="@OnAddressChanged"
Value="@Model.AddressModel">
</AddressForm>
...
答案1
得分: 0
父 EditContext 需要传递给子组件。它不能自己创建。如何做到这一点在这里回答。
英文:
World's shortest answer - the parent EditContext needs to be passed down to the child component. It cannot make its own.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论