Back to Devexpress

DxTagBox<TData, TValue>.TagsChanging Event

blazor-devexpress-dot-blazor-dot-dxtagbox-2-64e92bcc.md

latest3.5 KB
Original Source

DxTagBox<TData, TValue>.TagsChanging Event

Fires when a user is changing tags in the Tag Box. Use this event to validate/cancel user input or tag selection.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[Parameter]
public Action<ParameterValueChangingEventArgs<IEnumerable<string>>> TagsChanging { get; set; }

Event Data

The TagsChanging event's data class is ParameterValueChangingEventArgs<IEnumerable<String>>. The following properties provide information specific to this event:

PropertyDescription
NewValueGets or sets the new value being assigned to the parameter.
OldValueGets the current parameter value.

Remarks

The TagsChanging event fires before new tags are applied to the component (before the TagsChanged event). You can use this event to validate/cancel user input or tag selection.

The following code prevents selection of more than three tags. If a user tries to select a fourth tag, the Tag Box’s value remains unchanged.

razor
<DxTagBox Data="@Cities"
          AllowCustomTags="true"
          Tags="@Tags"
          TagsChanged="@TagsChanged"
          TagsChanging="@OnTagsChanging"
          @bind-Values="@Values">
</DxTagBox>

@code {
    IEnumerable<string> Tags = new List<string>() {
        "London",
        "New York"
    };

    IEnumerable<string> Cities = new List<string>() {
        "London",
        "Berlin",
        "Paris",
        "New York",
    };

    IEnumerable<string> Values;

    void TagsChanged(IEnumerable<string> newTags) {
        Tags = newTags;
    }

    void OnTagsChanging(ParameterValueChangingEventArgs<IEnumerable<string>> e) {
        if (e.NewValue != null && e.NewValue.Count() > 3) {
            e.NewValue = e.OldValue;
        }
    }
}

The following code allows users to add custom tags that contain the @ symbol and end with .com.

razor
@page "/"
@rendermode InteractiveServer

<DxTagBox Data="@emails"
          AllowCustomTags="true"
          @bind-Tags="@tags"
          TagsChanging="@TagsChanging"
          @bind-Values="@values">
</DxTagBox>

@code {
    IEnumerable<string> tags;

    IEnumerable<string> emails = new List<string>() {
        "[email protected]",
        "[email protected]",
        "[email protected]",
    };

    IEnumerable<string> values = new List<string>() {
        "[email protected]"
    };

    void TagsChanging(ParameterValueChangingEventArgs<IEnumerable<string>> newTagsEventArgs) {
        List<string> validValues = new List<string>();
        foreach (var tag in newTagsEventArgs.NewValue){
            if (tag.Contains("@") && tag.EndsWith(".com")){
                validValues.Add(tag);
            }
        }
        newTagsEventArgs.NewValue = validValues;
    }
}

See Also

DxTagBox<TData, TValue> Class

DxTagBox<TData, TValue> Members

DevExpress.Blazor Namespace