Search code examples
c#eventsblazorblazor-webassembly

How to listen to EditContext.NotifyFieldChanged event?


I have component which is wrapped around EditForm with the model set. EditForm contains in turn some components based on InputBase.

On change InbutBase triggers event EditContext.NotifyFieldChanged but I fail to spot connection between this event and EditForm. I already tried to listen to "onChange" on EditForm -- nothing.

So how to listen to this event?

I would like to listen to single event from entire form rather than listening to multiple events coming from multiple inputs within form.


Solution

  • I found the way -- I made the class managing the model IDisposable and added constructor:

    public Markers()
    {
      this.editContext = new EditContext(this);
      this.editContext.OnFieldChanged += EditContextOnOnFieldChanged;
    }
    
    public void Dispose()
    {
      this.editContext.OnFieldChanged -= EditContextOnOnFieldChanged;
    }
    
    private void EditContextOnOnFieldChanged(object? sender, FieldChangedEventArgs e)
    {
      ...
    }
    
    

    then instead of setting Model for EditForm I set EditContext which I just created.

    A bit twisted because I thought it will be possible to do solely within markup but I will live :-).