触发ValueChanged后如何更新MudDataGrid的关联列?
问题描述
我定义了如下MudDataGrid:
<MudDataGrid @ref="_dataGrid" Virtualize="true" Dense EditMode="DataGridEditMode.Cell" ReadOnly="false" T="InmodeloReposicion" Items="@modeloRepoDisp" SortMode="SortMode.Multiple" Filterable="true" Hideable="true" HorizontalScrollbar="false" FilterMode="@DataGridFilterMode.ColumnFilterRow" FilterCaseSensitivity="DataGridFilterCaseSensitivity.CaseInsensitive" @bind-SelectedItem="selectedItem"> ... <PropertyColumn Property="x => x.VentaMes12" Title="Venta Mes 12" IsEditable="false" CellStyle="max-width: 100px;" CellStyleFunc="_cellStyleFunc12" /> <TemplateColumn Title="Venta Atípica"> <EditTemplate Context="product"> <MudTextField T="int?" Value="@product.Item.ValoresAtipicos" Immediate="true" ValueChanged="@(newValue => ValChanged(newValue, product.Item) )" /> </EditTemplate> <CellTemplate Context="product">@product.Item.ValoresAtipicos</CellTemplate></TemplateColumn> ... </MudDataGrid>
对应的ValueChanged方法如下:
private void ValChanged(int? value, InmodeloReposicion a){ try { a.ValoresAtipicos = value; a.Mes12 = value; decimal? valores = a.ConsumoPromPorDia - a.ValoresAtipicos; a.ConsumoPromPorDia = Math.Max(a.ConsumoPromPorDia - a.ValoresAtipicos ?? 0, 0); } catch (Exception) { throw; } StateHasChanged(); }
Items是通过按钮点击填充的内存列表。修改“Venta Atípica”列的值后,希望更新其他关联列(如“Venta Mes 12”),但操作后无效果,调用StateHasChanged()也未解决问题,该如何处理?
解决方案
让实体实现INotifyPropertyChanged接口
MudDataGrid需要依赖属性变更通知来自动更新UI。如果InmodeloReposicion类没实现这个接口,哪怕修改了属性值,UI也无法感知到变化。修改实体类示例:using System.ComponentModel; using System.Runtime.CompilerServices; public class InmodeloReposicion : INotifyPropertyChanged { private int? _valoresAtipicos; private int? _mes12; private decimal? _consumoPromPorDia; public int? ValoresAtipicos { get => _valoresAtipicos; set { if (_valoresAtipicos != value) { _valoresAtipicos = value; OnPropertyChanged(); } } } public int? Mes12 { get => _mes12; set { if (_mes12 != value) { _mes12 = value; OnPropertyChanged(); } } } public decimal? ConsumoPromPorDia { get => _consumoPromPorDia; set { if (_consumoPromPorDia != value) { _consumoPromPorDia = value; OnPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }强制刷新DataGrid的目标行
如果不想修改实体类,可在ValChanged方法里调用DataGrid的RefreshItem方法,明确通知UI更新指定行:private void ValChanged(int? value, InmodeloReposicion a){ try { a.ValoresAtipicos = value; a.Mes12 = value; decimal? valores = a.ConsumoPromPorDia - a.ValoresAtipicos; a.ConsumoPromPorDia = Math.Max(a.ConsumoPromPorDia - a.ValoresAtipicos ?? 0, 0); } catch (Exception) { throw; } // 刷新当前修改的行 _dataGrid.RefreshItem(a); // 若仍无效,尝试刷新整个DataGrid // _dataGrid.Refresh(); }注意要保证
_dataGrid的@ref已正确绑定,且调用时不为null。排查虚拟滚动的影响
因为开启了Virtualize="true",虚拟滚动可能会缓存行数据。可以临时关闭Virtualize验证是否是这个原因导致的UI不更新,若关闭后正常,可在修改后调用_dataGrid.VirtualizeProvider.Refresh()来刷新虚拟列表。
内容的提问来源于stack exchange,提问作者George Arce
相关产品推荐
相关产品推荐

