Back to Devexpress

DxListBox<TData, TValue>.ValuesChanging Event

blazor-devexpress-dot-blazor-dot-dxlistbox-2-89f2ab42.md

latest3.5 KB
Original Source

DxListBox<TData, TValue>.ValuesChanging Event

Fires when the List Box’s selected values are being changed. Use this event to validate/cancel the newly selected 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 List 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/cancel the newly selected values. The following code prevents selection of restricted items. If a user tries to select an item that is marked as restricted, that item is excluded from the selection.

razor
<DxListBox Data="@Products"
           @bind-Values="@SelectedProducts"
           ValuesChanging="@OnValuesChanging"
           TextFieldName="@nameof(Product.Name)"
           SelectionMode="ListBoxSelectionMode.Multiple"
           ShowCheckboxes="true">
</DxListBox>

<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

DxListBox<TData, TValue> Class

DxListBox<TData, TValue> Members

DevExpress.Blazor Namespace