Back to Devexpress

DxTagBox<TData, TValue>.ValuesChanging Event

blazor-devexpress-dot-blazor-dot-dxtagbox-2-345dc81f.md

latest3.4 KB
Original Source

DxTagBox<TData, TValue>.ValuesChanging Event

Fires when the Tag Box’s selected values are being changed. Use this event to validate or cancel the new values.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[Parameter]
public Action<ParameterValueChangingEventArgs<IEnumerable<TValue>>> ValuesChanging { get; set; }

Event Data

The ValuesChanging event's data class is ParameterValueChangingEventArgs<IEnumerable<TValue>>. 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 Tag Box editor raises the ValuesChanging event before the ValuesChanged event. Both events fire in the following cases:

You can use the ValuesChanging event to validate or cancel the newly selected values. The following code prevents restricted product selection. If a user tries to select a product that is marked as restricted, that product is excluded from the selection.

razor
<DxTagBox Data="@Products"
          @bind-Values="@SelectedProducts"
          ValuesChanging="@OnValuesChanging"
          TextFieldName="@nameof(Product.Name)">
</DxTagBox>

<div class="mt-2">
    <span>Selected products: @(string.Join(", ", SelectedProducts?.Select(p => p.Name) ?? Array.Empty<string>()))</span>
</div>

@code {
    IEnumerable<Product> SelectedProducts { get; set; }
    IEnumerable<Product> Products { get; set; }

    protected override void OnInitialized() {
        Products = new List<Product> {
            new Product { Name = "Regular Item 1", IsRestricted = false },
            new Product { Name = "Premium Item", IsRestricted = true },
            new Product { Name = "Regular Item 2", IsRestricted = false }
        };
    }

    private void OnValuesChanging(ParameterValueChangingEventArgs<IEnumerable<Product>> e) {
        var oldValues = e.OldValue ?? Array.Empty<Product>();
        var newValues = e.NewValue ?? Array.Empty<Product>();

        // Exclude restricted items from selection
        var restrictedItems = newValues.Where(p => p.IsRestricted).ToList();
        if (restrictedItems.Any()) {
            e.NewValue = newValues.Except(restrictedItems).ToList();
        }
    }

    public class Product {
        public string Name { get; set; }
        public bool IsRestricted { get; set; }
    }
}

See Also

DxTagBox<TData, TValue> Class

DxTagBox<TData, TValue> Members

DevExpress.Blazor Namespace