在Blazor中,如何在动态模型中@bind并激发@onchange
型号: 如何在FilterChangedBrand方法中查找相关模型属性并将其设置为newCheckedBrand在Blazor中,如何在动态模型中@bind并激发@onchange,blazor,Blazor,型号: 如何在FilterChangedBrand方法中查找相关模型属性并将其设置为newCheckedBrand 或者在复选框标记中使用@bind=“@item.Checked”,然后在其中一个复选框的选中状态更改时引发一个事件?因为无法使用@bind和@onchange,您必须仅在代码中进行更改。最简单的方法是使用lambda来捕获项 剃刀 @foreach(Model.Brand.checkbox中的var项) { @项目.价值 } @代码 公共过滤器模型{get;set;}//已在On
或者在复选框标记中使用@bind=“@item.Checked”,然后在其中一个复选框的选中状态更改时引发一个事件?因为无法使用@bind和@onchange,您必须仅在代码中进行更改。最简单的方法是使用lambda来捕获
项
剃刀
@foreach(Model.Brand.checkbox中的var项)
{
@项目.价值
}
@代码
公共过滤器模型{get;set;}//已在OnParametersSet中初始化
公共事件行动过滤器已更改;
私有无效筛选器ChangedBrand(筛选器复选框项,ChangeEventArgs e)
{
//在这里你可以做@bind的工作
item.Checked=!item.Checked;
字符串newCheckedBrand=e.Value.ToString();
//现在,如何查找相关模型属性并将其设置为newCheckedBrand
过滤器已更改?.Invoke(模型);
}
另一种更复杂的方法是将事件级联放置在模型本身中,如果您希望将UI与WPF(例如WPF)一起重用,这种方法可能会有所帮助
public FiltersModel Model { get; set; } // Initialized in OnParametersSet
public event Action<FiltersModel> FiltersChanged;
private void FilterChangedBrand(FilterCheckBox item, ChangeEventArgs e)
{
// here you do work of @bind
item.Checked = !item.Checked;
string newCheckedBrand = e.Value.ToString();
// Now How to Find and Set the relevant Model property to newCheckedBrand
FiltersChanged?.Invoke(Model);
}
带有标题的公共类复选框列表
{
私有列表项=新列表();
公共IReadOnlyList复选框=>items.AsReadOnly();
公共事件事件处理程序模型已更改;
公共作废添加(过滤器复选框项)
{
item.CheckedChanged+=此.item\u CheckedChanged;
此.items.Add(item);
}
私有无效项\u CheckedChanged(对象发送方,事件参数e)
{
ModelChanged.Invoke(这个,EventArgs.Empty);
}
}
公共类过滤器检查盒
{
公共字符串值{get;set;}
公共布尔检查{get;set;}
公共事件事件处理程序CheckedChanged;
}
如您所见,CheckBoxListWithTitle将处理所需事件的传播。在Razor中,您仅订阅
CheckBoxListWithTitle.ModelChanged
另一个选项是在绑定对象中使用INotifyPropertyChanged接口
public class CheckBoxListWithTitle
{
private List<FilterCheckBox> items = new List<FilterCheckBox>();
public IReadOnlyList<FilterCheckBox> CheckBoxes => items.AsReadOnly();
public event EventHandler ModelChanged;
public void Add(FilterCheckBox item)
{
item.CheckedChanged += this.Item_CheckedChanged;
this.items.Add(item);
}
private void Item_CheckedChanged(object sender, EventArgs e)
{
ModelChanged.Invoke(this, EventArgs.Empty);
}
}
public class FilterCheckBox
{
public string Value { get; set; }
public bool Checked { get; set; }
public event EventHandler CheckedChanged;
}
然后在页面中,注册到ItemOnPropertyChanged事件
public class RowToApprove : INotifyPropertyChanged
{
private bool _approved;
public string AddressLine { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Approved
{
get => _approved;
set
{
if (value == _approved) return;
_approved = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
在那里做你的工作:
item.PropertyChanged += ItemOnPropertyChanged;
您的物品可能如下所示:
private void ItemOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Do something
}
我喜欢Lambda方法,谢谢!
public class CheckBoxListWithTitle
{
private List<FilterCheckBox> items = new List<FilterCheckBox>();
public IReadOnlyList<FilterCheckBox> CheckBoxes => items.AsReadOnly();
public event EventHandler ModelChanged;
public void Add(FilterCheckBox item)
{
item.CheckedChanged += this.Item_CheckedChanged;
this.items.Add(item);
}
private void Item_CheckedChanged(object sender, EventArgs e)
{
ModelChanged.Invoke(this, EventArgs.Empty);
}
}
public class FilterCheckBox
{
public string Value { get; set; }
public bool Checked { get; set; }
public event EventHandler CheckedChanged;
}
public class RowToApprove : INotifyPropertyChanged
{
private bool _approved;
public string AddressLine { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Approved
{
get => _approved;
set
{
if (value == _approved) return;
_approved = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
item.PropertyChanged += ItemOnPropertyChanged;
private void ItemOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Do something
}
<input type="checkbox" @bind-value="@(context.Approved)" />