Data Binding in Blazor Calendar Component

10 Feb 20221 minute to read

This section briefly explains how to bind the value to the Calendar component in the below different ways.

  • One-Way Data Binding
  • Two-Way Data Binding
  • Dynamic Value Binding

One-Way Binding

You can bind the value to the Calendar component directly for Value property as mentioned in the following code example. In one-way binding, you need to pass property or variable name along with @ (For Ex: “@DateValue”).

@using Syncfusion.Blazor.Calendars

<SfCalendar TValue="DateTime?" Value="@DateValue"></SfCalendar>

<button @onclick="@UpdateValue">Update Value</button>

@code {
    public DateTime? DateValue {get;set;} = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 28);

    public void UpdateValue()
    {
        DateValue = DateTime.Now;
    }
}

Two-Way data binding

Two-way binding can be achieved by using bind-Value attribute and it supports string, int, Enum, DateTime, bool types. If the component value has been changed, it will affect all the places where the variable is bound for the bind-value attribute.

@using Syncfusion.Blazor.Calendars

<p>Calendar value is: @DateValue</p>

<SfCalendar TValue="DateTime?" @bind-Value="@DateValue"></SfCalendar>

@code {
public DateTime? DateValue { get; set; } = DateTime.Now;
}

Dynamic value binding

You can change the property value dynamically by manually calling the StateHasChanged() method inside public event of Blazor Calendar component only. This method notifies the component that its state has changed and queues a re-render.

There is no need to call this method for native events since it’s called after any lifecycle method has been called. It can also be invoked manually to trigger a re-render. Please refer the below mentioned code example.

@using Syncfusion.Blazor.Calendars

<p>Calendar value is: @DateValue</p>

<SfCalendar TValue="DateTime?" Value="@DateValue">
    <CalendarEvents TValue="DateTime?" ValueChange="@onChange"></CalendarEvents>
</SfCalendar>

@code {

public DateTime? DateValue { get; set; } = DateTime.Now;

private void onChange(Syncfusion.Blazor.Calendars.ChangedEventArgs<DateTime?> args)
    {
        DateValue = args.Value;
        StateHasChanged();
    }
}