我在 EditForm 中有一个 SfListView。在模板部分的行 'var fileItem = (context as AttachmentFile);' 我收到下一个错误: CS0039 无法通过引用转换、装箱转换、拆箱转换、包装转换或 null 类型转换将类型“Forms.EditContext”转换为“Models.AttachmentFile”。继承、引用转换和装箱转换允许使用 as 运算符进行转换。
这是我的代码:
<EditForm EditContext="@myContext" OnSubmit="OnSendClick">
<SfListView EnableVirtualization="true" DataSource="@attachments">
<ListViewFieldSettings TValue="AttachmentFile" Id="@nameof(AttachmentFile.Id)" Text="@nameof(AttachmentFile.Name)">
</ListViewFieldSettings>
<ListViewTemplates TValue="AttachmentFile">
<Template>
@{
var fileItem = (context as AttachmentFile);
<div class="rezmessage-attachment-file">
<i class="@fileItem.Icon"></i>
<a href="@fileItem.WebPath" target="_blank">fileItem.Name</a>
</div>
}
</Template>
</ListViewTemplates>
</SfListView>
如何获取与 SfListView 相关的上下文,而不是与 EditForm 相关的上下文?
您可以使用模板元素上的
Context
属性 Ms docs 定义上下文的范围名称。
那么你甚至不需要施放它
(context as AttachmentFile);
<EditForm EditContext="@myContext" OnSubmit="OnSendClick">
<SfListView EnableVirtualization="true" DataSource="@attachments">
<ListViewFieldSettings TValue="AttachmentFile" Id="@nameof(AttachmentFile.Id)" Text="@nameof(AttachmentFile.Name)">
</ListViewFieldSettings>
<ListViewTemplates TValue="AttachmentFile">
<Template Context="templateContext">
@{
var fileItem = templateContext;
<div class="rezmessage-attachment-file">
<i class="@fileItem.Icon"></i>
<a href="@fileItem.WebPath" target="_blank">@fileItem.Name</a>
</div>
}
</Template>
</ListViewTemplates>
</SfListView>
</EditForm>