自定义 Blazor 组件未正确反映绑定
Custom Blazor Component not reflecting binding properly
我有一个很大的 DxFormLayout
有很多 DxFormLayoutGroup
,每个 DxFormLayoutGroup
有很多 DxFormlauoutItem
。我想知道是否可以通过组件化几个 DxFormlauoutItem
来压缩剃刀代码。这是我的代码:
页数:
<EditForm Model="@_model" Context="editFormContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<DxFormLayout CssClass="dxFormLayoutHeaderStyle">
<DxFormLayoutGroup Caption="Options" ColSpanMd="12">
<DxFormLayoutItem Caption="" ColSpanMd="12">
@*The below is repeated multiple times*@
<Template>
<DxStackLayout ItemSpacing="10px">
<Items>
<DxStackLayoutItem Length="95%">
<Template>
<DxTextBox ReadOnly="true" @bind-Text="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"
title="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" SizeMode="SizeMode.Medium"/>
</Template>
</DxStackLayoutItem>
<DxStackLayoutItem Length="5%">
<Template>
<div class="stacklayout-item">
<DxCheckBox CheckType="CheckType.Switch" style="width: 100%" @bind-Checked="@_model.IsNegotiationsDoneBySupplyManagement"
Alignment="CheckBoxContentAlignment.Center" title="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"/>
</div>
</Template>
</DxStackLayoutItem>
</Items>
</DxStackLayout>
</Template >
@*The above is repeated multiple times*@
</DxFormLayoutItem >
</DxFormLayoutGroup>
</DxFormLayout>
</EditForm>
@*The below become a component with parameters and bindings*@
<EditForm Model="@_model" Context="editFormContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<DxFormLayout CssClass="dxFormLayoutHeaderStyle">
<DxFormLayoutItem Caption="" ColSpanMd="12">
<Template>
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"
DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
</Template >
</DxFormLayoutItem >
</DxFormLayout>
</EditForm>
分量:
<style>
.stacklayout-item {
text-align: center;
height: 100%;
padding-top: 6px;
}
</style>
<DxStackLayout ItemSpacing="10px">
<Items>
<DxStackLayoutItem Length="95%">
<Template>
<DxTextBox ReadOnly="true" @bind-Text="@DecisionText" title="@DecisionText" SizeMode="SizeMode.Medium"/>
</Template>
</DxStackLayoutItem>
<DxStackLayoutItem Length="5%">
<Template>
<div class="stacklayout-item">
<DxCheckBox CheckType="CheckType.Switch" style="width: 100%" @bind-Checked="@DecisionResult"
Alignment="CheckBoxContentAlignment.Center" title="@DecisionResult"/>
</div>
</Template>
</DxStackLayoutItem>
</Items>
</DxStackLayout>
@code {
[Parameter]
public string DecisionText
{
get => _decisionText;
set
{
if (_decisionText == value) return;
_decisionText = value;
DecisionTextChanged.InvokeAsync(value);
}
}
[Parameter]
public bool DecisionResult
{
get => _decisionResult;
set
{
if (_decisionResult == value) return;
_decisionResult = value;
DecisionResultChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<string> DecisionTextChanged { get; set; }
[Parameter]
public EventCallback<bool> DecisionResultChanged { get; set; }
private string _decisionText;
private bool _decisionResult;
}
问题:
我把它变成了剃刀组件,但我遇到了问题,因为模型的属性没有在主页上更新。我可以通过一个 属性 来确认这一点:在页面上,一旦 model.IsNegotiationsDoneBySupplyManagement
设置为 true,就会启用一个 SpinEdit
。一旦我进入组件模式,这种情况就不再发生了:
<DxSpinEdit Id="amountSavedAfterNegotiations" @bind-Value="@_model.SavingsAfterNegotiations" Enabled="@_model.IsNegotiationsDoneBySupplyManagement" title="Savings (AED) after negotiations?" />
当我有原始代码(没有我粘贴的 component/top-most 代码)时,选中此复选框将切换 SpinEdit
的 Enabled
状态。在我转移到组件后,Enabled
状态不再感知 model
的 属性 的变化,这让我相信页面上的 model
的属性不是正在更新。
我连接组件的方式有什么问题?
缺少的魔法在我调用组件的页面上。这就是我在做的事情:
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
这是正确的语法:
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" @bind-DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
我不得不将 DecisionResult
更改为 @bind-DecisionResult
。现在 Page
的 model
反映了其属性在组件中发生的变化。
我有一个很大的 DxFormLayout
有很多 DxFormLayoutGroup
,每个 DxFormLayoutGroup
有很多 DxFormlauoutItem
。我想知道是否可以通过组件化几个 DxFormlauoutItem
来压缩剃刀代码。这是我的代码:
页数:
<EditForm Model="@_model" Context="editFormContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<DxFormLayout CssClass="dxFormLayoutHeaderStyle">
<DxFormLayoutGroup Caption="Options" ColSpanMd="12">
<DxFormLayoutItem Caption="" ColSpanMd="12">
@*The below is repeated multiple times*@
<Template>
<DxStackLayout ItemSpacing="10px">
<Items>
<DxStackLayoutItem Length="95%">
<Template>
<DxTextBox ReadOnly="true" @bind-Text="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"
title="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" SizeMode="SizeMode.Medium"/>
</Template>
</DxStackLayoutItem>
<DxStackLayoutItem Length="5%">
<Template>
<div class="stacklayout-item">
<DxCheckBox CheckType="CheckType.Switch" style="width: 100%" @bind-Checked="@_model.IsNegotiationsDoneBySupplyManagement"
Alignment="CheckBoxContentAlignment.Center" title="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"/>
</div>
</Template>
</DxStackLayoutItem>
</Items>
</DxStackLayout>
</Template >
@*The above is repeated multiple times*@
</DxFormLayoutItem >
</DxFormLayoutGroup>
</DxFormLayout>
</EditForm>
@*The below become a component with parameters and bindings*@
<EditForm Model="@_model" Context="editFormContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<DxFormLayout CssClass="dxFormLayoutHeaderStyle">
<DxFormLayoutItem Caption="" ColSpanMd="12">
<Template>
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement"
DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
</Template >
</DxFormLayoutItem >
</DxFormLayout>
</EditForm>
分量:
<style>
.stacklayout-item {
text-align: center;
height: 100%;
padding-top: 6px;
}
</style>
<DxStackLayout ItemSpacing="10px">
<Items>
<DxStackLayoutItem Length="95%">
<Template>
<DxTextBox ReadOnly="true" @bind-Text="@DecisionText" title="@DecisionText" SizeMode="SizeMode.Medium"/>
</Template>
</DxStackLayoutItem>
<DxStackLayoutItem Length="5%">
<Template>
<div class="stacklayout-item">
<DxCheckBox CheckType="CheckType.Switch" style="width: 100%" @bind-Checked="@DecisionResult"
Alignment="CheckBoxContentAlignment.Center" title="@DecisionResult"/>
</div>
</Template>
</DxStackLayoutItem>
</Items>
</DxStackLayout>
@code {
[Parameter]
public string DecisionText
{
get => _decisionText;
set
{
if (_decisionText == value) return;
_decisionText = value;
DecisionTextChanged.InvokeAsync(value);
}
}
[Parameter]
public bool DecisionResult
{
get => _decisionResult;
set
{
if (_decisionResult == value) return;
_decisionResult = value;
DecisionResultChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<string> DecisionTextChanged { get; set; }
[Parameter]
public EventCallback<bool> DecisionResultChanged { get; set; }
private string _decisionText;
private bool _decisionResult;
}
问题:
我把它变成了剃刀组件,但我遇到了问题,因为模型的属性没有在主页上更新。我可以通过一个 属性 来确认这一点:在页面上,一旦 model.IsNegotiationsDoneBySupplyManagement
设置为 true,就会启用一个 SpinEdit
。一旦我进入组件模式,这种情况就不再发生了:
<DxSpinEdit Id="amountSavedAfterNegotiations" @bind-Value="@_model.SavingsAfterNegotiations" Enabled="@_model.IsNegotiationsDoneBySupplyManagement" title="Savings (AED) after negotiations?" />
当我有原始代码(没有我粘贴的 component/top-most 代码)时,选中此复选框将切换 SpinEdit
的 Enabled
状态。在我转移到组件后,Enabled
状态不再感知 model
的 属性 的变化,这让我相信页面上的 model
的属性不是正在更新。
我连接组件的方式有什么问题?
缺少的魔法在我调用组件的页面上。这就是我在做的事情:
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
这是正确的语法:
<BudgetReleaseRequestDecisionPoint DecisionText="@_model.TooltipTextForNegotiationsDoneBySupplyManagement" @bind-DecisionResult="@_model.IsNegotiationsDoneBySupplyManagement" />
我不得不将 DecisionResult
更改为 @bind-DecisionResult
。现在 Page
的 model
反映了其属性在组件中发生的变化。